Manually set the state of a sensor

Why is there no service to set the state of a sensor?

I know there is at least one Python script available in the forums to do it but it should be native to HA.
(e.g. How to manually set state/value of sensor?)

Iā€™m interested in seeing a use-case for this. In my view ā€˜sensorsā€™ are there to give the value of a real-world ā€˜thingā€™. Setting this manually is overriding the real-world valueā€¦ why?

For testing purpose during development this would be nice. I think.

Yes, my use case isnā€™t earth shattering butā€¦

I have some door and window sensors that arenā€™t 100% accurate so I like to be able to change their state.
And yes, this is a solution to a ā€˜separateā€™ problem as well, I get that but Iā€™m guessing other people have other use cases else the python script wouldnā€™t have been written :wink:

2 Likes

I think this is possible with the ā€˜Develeper Toolsā€™, if you just want to adjust the states manually some times when they are wrong.

Develeper Tools > States > Select your entity > Change the state > Click blue button ā€˜Set stateā€™

Yes it is.
Currently though I have a button that shows the state of each door window. If it is in the wrong state I can now double click and have the Python script run to set it. It would just be nice to have it as a native service.

Well the REST API has a set-state API so you could do it. I havenā€™t tried it myself but I would think you could make a button that calls a rest command which calls into that API to change the state.

Although I think in general HA tries very hard to avoid manual state changes to sensor because it wants to be able to explain its state machine from the logic. Once you introduce a lot of manual state changes to sensors it becomes difficult to determine how an entity got in the state its in.

An alternative approach here could be to create a template sensor for each of your sensors that donā€™t have the most accurate results so you can then apply your own corrective logic to it. Or if you really want to you can make an input_* (not sure which type of sensor it is) and just have your template sensor set its value from either hte sensor or the input, whichever was last updated. That would then give you full manual control.

1 Like

I have a use case, but it is probably not very common.
I have a timer for ā€œlights offā€ X minutes after a motion sensor turns off. But if I change the timer, say from 30 minutes after motion off to 10 minutes after motion off, some time between 10 and 30 minutes after the sensor was turned off, the trigger will not run, as 10 minutes have already passed.
So when I modify the timer while the light is on, I temporarily set the motion sensor to on for a second to refresh the timeout.

1 Like

This could be achieved in a nicer way by having the state-change of the timer value be a trigger for the countdown as well as the motion sensor

I did not go into details, but the point is that I rely on the time a binary_sensor has been in the ā€œoffā€ state for an automation to run.
Basically a

trigger:
  entity_id: binary_sensor: foo
  to: 'off'
  for: "{{ template that returns a timestamp }}"

And if the original time was 30 minutes, and it now is 10 minutes since the sensor turned off (so it should trigger in 20 minutes), and something changes, so the template instead of returning ā€œ00:30:00ā€ returns ā€œ00:02:00ā€, the automation will never run.

A simple

    - service: python_script.set_state
      data_template:
        entity_id: binary_sensor.foo
        state: "on"
    - delay: '00:00:01'
    - service: python_script.set_state
      data_template:
        entity_id: binary_sensor.foo
        state: "off"

Ensures that the automation will fire in 2 minutes

I have a real-world use case for this feature.

Below is my automation to toggle a kitchen light switch via RF wall switch thru Sonoff RF Bridge (flashed with Tasmota, using MQTT to connect to HA).

- alias: Kitchen Toggle
  trigger:
    platform: state
    entity_id: sensor.rf_bridge
    to: '2B10D4'    # Kitchen RF Wall Switch
  action:
    service: switch.toggle
    entity_id: switch.kitchen_light   #Kitchen Light
  id: Kitchen_Toggle

Problem: First press, it toggles. Second press, nothing happens because the state doesnā€™t change (itā€™s still the same RF value).
Workaround: Using delay 1 second, set state manually to something else (like a -).

- alias: Kitchen Toggle
  trigger:
    platform: state
    entity_id: sensor.rf_bridge
    to: '2B10D4'    # Kitchen RF Wall Switch
  action:
  - service: switch.toggle
    entity_id: switch.kitchen_light   #Kitchen Light
  - delay:
      seconds: 1
  - service: python_script.sensor_state_reset
  id: Kitchen_Toggle
hass.states.set('sensor.rf_bridge', '-', hass.states.get('sensor.rf_bridge').attributes.copy())

I dream for a day to come where I could just set state directly without another automation or python script.

Uh all you need to use is the expire_after field for MQTT and your state will expire. No need for an automationā€¦

or force_update

Oh wow. expire_after did the trick. Many thanks.

1 Like

Thereā€™s so much buried in MQTTā€¦ Iā€™m convinced it can do everything. When in doubt with MQTT, check the docs.