I get what you guys mean with the “unknown” state at Home Assistant reboot, I have the same.
And in my automations I did a test of the button going from “OFF” to “ON” but the button is only a pushbutton and will never send an OFF signal to RF.
Result after reboot HA the state is OFF, when I push the button it goes to “ON” and because I set an off_delay of 1 after one second the state will go to OFF.
Issue: the first time after reboot the button will not work as it goes from “Unknown” to “ON” and not “OFF” to “ON”
Example of 1 of my buttons:
- platform: mqtt
state_topic: "home/sonoffRemoteB1"
name: 'Sonoff Remote Button 1'
off_delay: 1
Python script:
d = { 'code8E7218':['sonoffRemoteB1','ON','false']
}
p = data.get('payload')
if p is not None:
if p in d.keys():
service_data = {'topic':'home/{}'.format(d[p][0]), 'payload':'{}'.format(d[p][1]), 'qos':0, 'retain':'{}'.format(d[p][2])}
else:
service_data = {'topic':'home/unknown1', 'payload':'{}'.format(p), 'qos':0, 'retain':'false'}
logger.warning('<rfbridge_demux1> Received unknown RF command: {}'.format(p))
hass.services.call('mqtt', 'publish', service_data, False)
The Solution to get an “OFF” state in stead of “Unknown” after reboot of Home Assistant:
Make a script to set the state of the devices to “OFF”
s:
alias: Startup RF Devices
sequence:
- service: mqtt.publish
data:
topic: home/sonoffRemoteB1
payload: 'OFF'
mode: single
add an automation that runs after startup of Home Assistant:
- id: '1549662264057'
alias: Home Assistant ON
trigger:
- event: start
platform: homeassistant
action:
- service: script.s
data: {}
Ofcourse you can add the MQTT publish directly in the startup-automation, but I like to keep it organised.
Hope this helps you guys.