xannor / hass_py_set_state

Home Assistant Python Script to force set an entity state
The Unlicense
44 stars 14 forks source link

Request for an improvement #6

Closed Merangle closed 3 years ago

Merangle commented 3 years ago

Hello I install this script and it works well with this service

entity_id: sensor.jeedom_camille_nut_present state: 'test'

but it does not work well with this service

entity_id: sensor.jeedom_camille_nut_present state: {{states('sensor.start_camille_present')}} I get this information in the sensor : {'[object Object]': None}

Do you know if it's possible to get an improvment ? many thanks in advance

xannor commented 3 years ago

I ma not sure where I could improve it. The script is fairly simple and just passes through the values provided. it doesn't even handle the templating, it leaves that to HA. Also, states('sensor.start_camille_present') should be providing only the value and it looks like the {'[object Object]': None} is a serialized json object so I feel the sensor, sensor.start_camille_present, state is not what you expect. It is also possible, though I have not tested this, that HA's template engine reacts differently depending on how you call it. By default if you want to template the parameters you should use a data_template: block, for example:

data_template:
  entity_id: sensor.jeedom_camille_nut_present
  state: {{states('sensor.start_camille_present')}}

It could be that the recent changes to the HA template engine do not apply the same when not using the data: block.

Merangle commented 3 years ago

Hello

Thanks a lot for your quick answer. I've tried with your suggestion. Unfortunately it doesn' solve the issue.

Using developper tool / services with entity_id: sensor.jeedom_patricia_miband_room data: state: {{states('sensor.start_patricia_miband')}}

I still have the same message ' {'[object Object]': None}' While developper tool / template gives 'Salon' as the answer to {{states('sensor.start_patricia_miband')}}

and I have this message in the log

2020-12-31 15:21:33 INFO (SyncWorker_6) [homeassistant.components.python_script] Executing set_state.py: {'entity_id': 'sensor.jeedom_patricia_miband_room', 'state': {'[object Object]': None}}

Just to investigate further, can you please let me know if you are able to reproduce the issue ? Many thanks for your help And have an happy new year

xannor commented 3 years ago

Without knowing what the sensor you are templating off of is, I really cannot reproduce the issue. Also, the debug line you provided states that it is calling the script with the state set to {'[object Object]': None} which means that is what HA is passing to the set_state.py script, so at that point it is already in the format you do not want. There is nothing I can make the script do to cleanup bad input.

What do you get if you go to the developer tools/template and paste in your template? If it outputs the same result, you will have to contact the creator of the sensor to get it fixed.

Merangle commented 3 years ago

Hello

Thanks for your help.

Let me summarize the case. I have two sensors:

  1. 'sensor.jeedom_patricia_miband_room' (Mqtt sensor) which is populated with the name of the room where Patricia is located (Bureau, Salon, ....). The information is provided by an external system (Jeedom, french home assistant). But the sensor is populated only when there is an update in jeedom (which happens only when the Patricia moves from one room to another).

  2. 'sensor.start_patricia_miband' which is another sensor (rest query sent to Jeedom) to read the current location. The value is the same than the sensor 'sensor.jeedom_patricia_miband_room'.

But when I restart HA, 'sensor.jeedom_patricia_miband_room' status is set to unknown, until the first move of Patricia. Therefore, when HA starts I want to update the current location of Patricia with the location obtained with the second sensor. At start, the value of 'sensor.start_patricia_miband' is (e.g) Salon, and the value of 'sensor.jeedom_patricia_miband_room' is unknown. In the developper tool /template the value of {{ state('sensor.start_patricia_miband') }} is Salon.

And I want to copy the value of state('sensor.start_patricia_miband') in the sensor 'sensor.jeedom_patricia_miband_room' to get the current location of patricia when HA starts.

With the following command: - service: python_script.set_state entity_id: sensor.jeedom_patricia_miband_room data: state: {{ states('sensor.start_patricia_miband') }}
If I use the developper too /service to simulate this command, the sensor 'sensor.jeedom_patricia_miband_room', I get {'[object Object]': None}

If it still unclear or some information are missing, let me know. Any way many thanks for your help.

xannor commented 3 years ago

Thank you for the explanation, it definitely helped me understand your issue. One other question, you said that the output, from your template in the devtools, was Salon. What did it say the result type was? Did it say; Result type: string, or did it have another output. Also for your service call, which I assume is in an automation or script, you should be calling it this way:

- service: python_script.set_state
  data_template:
    entity_id: sensor.jeedom_patricia_miband_room
    state: {{ states('sensor.start_patricia_miband') }}

I believe that you have to use data_template: (or at least data:) as a sub-part to properly call the service. I think if you just use data: and have a state: {{}} it will try to interpret it as a JSON string, where-as data_template tells it to parse a template. I don't know if the manual service call via dev-tools supports passing a template as I think it is fixed to the data: tag.

Merangle commented 3 years ago

To answer your question. The output in devtools is a string.

Most important your suggestion about the service call was the answer. The issue is solved. Many thanks for your help and sorry for the trouble.

Once again, happy new year.