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?)
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
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.
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.
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.
Thereās so much buried in MQTTā¦ Iām convinced it can do everything. When in doubt with MQTT, check the docs.