How to manually set state/value of sensor?

@rodpayne

This is awesome. Any plans to put set_state.py on github? I’d love to be able to follow it in the event you make any future enhancements.

I put it under https://github.com/rodpayne/home-assistant.

6 Likes

Thanks for a nifty script! I was thinking to write an appdaemon for the exact same thing but python_script is much better option. It can be used from HA automation :slight_smile:.

Hi all.
I would love to test the script but I’m running HASSIO. Where should I copy the python_scripts to ???
Thanks!

/config/python_scripts

@rodpayne +1 for a PR. I agree that it should be part of HA. Had I seen this, it would have saved me a great deal of pain recently working out how to use the REST API to set entity attributes.

1 Like

I like this very much.
But I get a failure if the sensor is unknown.

:   File "set_state.py", line 15, in <module>
: TypeError: 'NoneType' object is not callable

Would it be possible to allow the script to create a new sensor if it does not already exist?

2 Likes

Could you tell us what sensor should be created?

Example: The code below tries to set a sensor ‘cold_water_rate’.
If such a sensor already exists in ha, then set_state would change its state to 42.
But if the sensor does not exist, then I get an error. To me it would make sense if set_state could be updated to check for this condition, and if needed automatically create a new sensor for the caller.
Perhaps it is easy, perhaps not, I don’t know.

- id: automation 3
  trigger:
    platform: time_pattern
    minutes: '/1'
  action:
    service: python_script.set_state
    data_template:
      entity_id: sensor.cold_water_rate
      state: 42

Well, I can see a reason in checking that entity does exist before changing its state.

you can use the following code:

inputEntity = data.get('entity_id')
if inputEntity is None:
    logger.warning("===== entity_id is required if you want to set something.")
elif hass.states.get(inputEntity) is None:
    logger.warning("===== unknown entity_id: %s", inputEntity)
else:
    inputStateObject = hass.states.get(inputEntity)
    inputState = inputStateObject.state
    inputAttributesObject = inputStateObject.attributes.copy()

    for item in data:
        newAttribute = data.get(item)
        logger.debug("===== item = {0}; value = {1}".format(item,newAttribute))
        if item == 'entity_id':
            continue            # already handled
        elif item == 'state':
            inputState = newAttribute
        else:
            inputAttributesObject[item] = newAttribute

    hass.states.set(inputEntity, inputState, inputAttributesObject)

On the other hand, I have no idea what one would create if a sensor does not exist… and who would use it… :wink:

1 Like

I don’t think would be possible because you wouldn’t be providing enough data (ie: attributes) to create the sensor.

1 Like

Is it possible that the status that you want to give with the set state comes from another sensor?

why not, provided that sensor does exist… :wink:

I could not use state: sensor.randomsenor though, but resolved in a different way.

Sir, this works like a charm, thanks heaps! What a nice script !

This is what I’ve done to set a sensor value based on a light switch state:

  sensors:
    ledstrip_consuption:
      value_template: >
        {% if is_state('light.led_strip', 'off') %}
          0
        {% else %}
          30
        {% endif %}

Hi,

im using your script to set the state of a switch but in the interface, it changed the toggle button for a string that say “true”,

any idea ?

here is the last part of the automation:

- service: python_script.set_state
  data_template:
    entity_id: switch.shelly1_basement
    state: ON

screenshot :

2019-07-13%2017_26_44-Home%20Assistant%20-%20Slimjet

have you tried

state: 'ON'

instead?

and by the way, why don’t you use switch.turn_on service?

Yes I tried that. It will show ‘on’ instead of true.
I’m not using turn_on because I don’t want to turn them on.
The reason is that I have 5 wifi lights that I control with a wifi switch but the switch itself never cut the power to the lights so I can control them at any time. Instead I’m using mqtt to turn off all lights using the switch. My problem is that if I open one lights. It will not put the state of the switch to on. Does it makes sense?

I’m afraid it doesn’t…
What is “wifi switch” and “open one light”?
It would be easier if you described your configuration in more details.