Automate setting an entity state?

If the RF bridge doesn’t publish its payloads as a retained message, the payload is not stored on the MQTT Broker. When Home Assistant restarts it re-connects to the MQTT Broker and receives no information about the binary_sensor’s current value. As of 2022.2 that situation is reported, quite truthfully, as unknown.

In previous versions, it wouldn’t reflect the actual situation (that it didn’t know the device’s state because it didn’t receive any information) but simply fake it and report (without any evidence) that it’s off.

This issue of “not knowing” is eliminated if the RF bridge publishes its payload as a retained message because it’s stored on the MQTT broker. On startup, Home Assistant connects to the broker and immediately receives the stored value.

I understand. I wasn’t able to make the RF Bridge send MQTT with the retain flag. I’m not learning intricacies of Tasmota rules and payloads for this. Tried with help of Tasmota Discord, couldn’t do it.

Fair enough; it’s why I stated earlier that the problem goes away if you’re using one of the two strategies in the linked post (it re-publishes the received data as a retained message). Anyway, that ship has sailed because you’re eliminating the RF bridge.

You should known that the unknown state is now a valid state for all binary_sensors. So don’t be too shocked if you see it appear elsewhere, like with the Zigbee sensors you’re purchasing.

Yes, I’m aware of that, thanks. At the very least the Aqara door sensors report the actual state.

You could technically store the state prior to restart in a variable (using a custom integration called “hass-variables” that survives restarts) and then set those entities to those states after startup using the set_state script.

the only time that it wouldn’t be reliable is on the very rare occasion that the door, etc gets opened/closed during the time it takes for HA to startup.

TBH, if I had this issue I would likely do it that way myself. especially if it was just a few entities. And it’s cheaper than buying new devices since the code is free. :laughing:

1 Like

I’ll probably repurpose them for something else, I mean the Zigbee sensors are $16 for two of them. I don’t want to waste any more time on this stuff. I love tinkering with HA but this was getting a bit much :slight_smile:

So do your RF binary_sensors. The difference is how Home Assistant integrates with them (meaning how the integration handles device information, specifically on startup).

Which Zigbee integration are you using?

Zigbee2MQTT

The irony is that it’s subject to the same issue that caused you grief with the RF bridge, namely how payloads are published to the MQTT broker (retained or not).

Ensure your devices, in Zigbee2MQTT’s configuration.yaml file, are configured appropriately.

https://www.zigbee2mqtt.io/guide/configuration/devices-groups.html

A bit of background here:

1 Like

Oh thanks for that. At the very least, retain is easy to enable on Zigbee2MQTT, and I have much better experience with it than with RF.

I have the same problem after updating to 2022.2. My RF bridge sensors all show ‘unknown’ after a HA restart. Using the demuxing strategy for rf bridge receiving data and retaining them only works if the sensor both sends the ‘on’ and ‘off’ state like the door contact sensors. But motion sensors only sends ‘on’ when motion is detected and never off. This means those sensors stay in ‘unknown’ state until motion happens. And the off_delay does not work with ‘unknown’ states. So it would really help if at HA startup a script can force the unknown entity states to a default known state, whatever that may be.
It will maybe contradict with the HA philosophy, but at least let this be possible with a script for those who explicitly want this behaviour (forcing the state of entities).

This works https://github.com/xannor/hass_py_set_state

It allows you to force entity states. I just run that on HA restart.

1 Like

Thanks, that helped.

1 Like

Hi,

I’m not sure what I’m doing wrong. 1. I couldn’t figure out how to install the python script via a HACS repository so I manually installed it. 2. I cant get it to reset my motion detector (the developer tools menu works fine). Here’s the code I’m using:

service: python_script.set_state
data: 
  entity_id: binary_sensor.smart_motion_sensor
  state: off

Ive tried off, 0, ‘off’ and false in the state param with no success. Any help appreciated. PS: It seems crazy that there is a developer tool that we can use in the UI but not programatically!
Any help appreciated :slight_smile:

Never got this to work.
Here is and alternate answer for others with the same issue:

shell_command:
  disarm_motion_: 'curl -X POST -H "Authorization: Bearer <your_token>" -H "Content-Type: application/json" -d ''{"state": "Clear"}'' http://xxx.xxx.xxx.xxx:8123/api/states/binary_sensor.smart_motion_sensor'

This works great, thanks! I’ve looking for something like this to set the status of my “smart” shutter to something other than unknown. It never occurred to me to set it via API calls. :smile:

It is frustrating that there isn’t a baked-in command for setting the initial state of sensors. My smoke alarms come up as ‘unknown’ until smoke detection fires off an MQTT message. ‘Unknown’ bothers me, I want it to say ‘clear’. Unless there’s smoke, of course.

So, here’s the solution:

  1. Get the set_state.py python script
  2. Save the script in config/python_scripts (you’ll have to create this directory)
  3. Add the line “python_script:” to your configuration.yaml
  4. Create an automation that calls the service “python_script.set_state”, with the data “entity_id: [name of your sensor entity]” and “state: [desired state text]”.

Your automation (viewed in YAML editor) should look something like this:

alias: Init Sensors
description: Set initial state at HA boot for sensors that only update on change
trigger:
  - platform: homeassistant
    event: start
action:
  - service: python_script.set_state
    data_template:
      entity_id: sensor.smokebsmt
      state: Clear
mode: single
2 Likes

What good is it to set an entity state?
Home Assistant Developer Tools → States clearly says: “Set the current state representation of an entity within Home Assistant. If the entity belongs to a device, there will be no actual communication with that device.
So setting a state doesn’t do anything, as far as I understand…

Yep. That’s correct.

:slight_smile: well, then why did someone bother to make a python_script.set_state when setting the state does nothing ?