Setting state of sensor from automation?

Is it possible to set the state of a binary or numeric sensor? What service i need to call to set it? I tried homeassistant/toggle for setting state of binary sensor but did not work…

Sensors are read-only devices, whether tied to a physical device or simply a virtual sensor, they are still read-only. There is a way to change the state basically in the memory of Home Assistant, but it is only momentary as the sensor will change back on the next polling, so it is not really being changed at all because it will not stick.

The homeassistant service (homeassistant.toggle, homeassitant.turn_on, etc) are only for switches, lights, scripts… things that can be acted upon, it won’t work for sensors.

As far as I know, you can not change the state of sensors through the frontend or directly through the services, but rather you can only set state values of sensors through the following API call:

/api/states/<entity_id>

But again, this is only momentary, as it will be changed back on the next polling of the sensor to retrieve its actual value. If this is a template sensor that is not being update externally, and is simply there for display purposes, then yes, setting its state will remain as there is nothing to poll in order to change it.

If you want sensors that you can change the values of on the fly, might I suggest creating MQTT sensors, then you can update them as often as you like.

If you could give us more information, such as why exactly you want to manually update the values of your sensors, perhaps we can give you alternative options to achieve the same results you are seeking.

Thanks for the clarification…i will check the API, my issue here i have a binary_sensor that is based on/updated via mqtt to ON based on my condition. But i have no way of triggering my OFF condition via MQTT. Hence i want to manually set the sensor state to OF via automation. Eg. after 10 min.
If you need more info i can write out a detailed post when i get home.

it is possible to create a commandline sensor who reads his value from a file.
and it is possible to make command lines (commandline switch, scripts, automations) which call a code that changes that same file.

with appdaemon you can do such things quite easy and you can also create a sensor which you can update from out off appdaemon (none supported function (yet)).

i am not familiar with MQTT, but i think is what you want to do is to set the value from the sensor. (not in HA but the actual sensor.)
maybe you can do that with a small py script that you then can call from out of HA?

I guess I’m not fully understanding your situation. Why are you not able to tigger your off condition via MQTT?

Can’t you just use the mqtt.publish service in your automation?

action:
  service: mqtt.publish
  data:
    topic: 'my/cool/topic'
    payload: 'OFF'
    retain: 'true'
1 Like

Yes! I didn’t think of that. Publishing via mqtt will fix my issue.
Thanks a lot for the help. :slight_smile:

We have an example of where setting sensor state would be useful:

If I’m reading this thread correctly the Zipato keypad both sets a sensor value to show its state but also updates itself if that sensor is different to itself. If we arm the alarm in HASS through some other method than using the keypad then the alarm state is different in HASS to the state in the keypad. Currently we’re using an appdaemon app to set the sensor state based on the ha_alarm state so when the keypad wakes up it syncs to HASS.

If we could set the sensor value via an automation this would set the keypad to the right state next time it woke up and we wouldn’t need appdaemon.

2 Likes

I have a similar need.

When my media centre PC is about to go to sleep, it activates a screen saver which sets the value of an HTTP Binary Sensor to “off”.

I listen for this in an automation, and tell my harmony remote to turn my TV off.

In the same automation, I need to reset the sensor back to “on” so that later, the media centre can turn it off again.

Is there a way to set it to “on” in the automation? Am I going about this whole thing the wrong way?

EDIT: Yep. I should just be calling a script from the API (API service documenation), not setting a sensor and then listening for the sensor.