If you just want to turn off the switches on reboot, you don’t have to do it this way.
All you have to do is to set restore_mode on the switch directly:
switch:
- platform: gpio
pin: 25
name: "Some switch"
restore_mode: ALWAYS_OFF #This will make it be turned off after reboot
The failsafe I was talking about was for more of a situation in case you have some kind of a HW button/switch connected directly to ESP to turn the irrigation on and you forget to turn it off after some time, this is where this manual failsafe delay would be useful. This way you could make the ESP standalone without the need for HA. You can make the sensor publish it’s state using lambda:
id(ha_delay).publish_state(10000); #To set it by default to 10s
But I am not sure if possibly there could be a case when the HA could connect earlier and sync it’s state with the sensor before the on_boot procedure is called, which would then overwrite the setting from the HA to the default value, but I think there should be some periodic sync aswell, this would have to be tested, but most probably it would be ok.
Another way would be to use global variable: more info here
globals:
- id: failsafe_delay_int
type: int
restore_value: no
initial_value: '10000'
where you would set the initial_value to the default. Then you would use the on_value
trigger on the HA sensor, which would set the globals variable to the current value of the HA sensor, and then use the global variable to read in the delay.
You could even set the restore_value
to on
and it should use the last value set by the HA even after reboot without HA. (keep it mind that ESP has limited count of overwrites in the flash memory, but in this case if you don’t change the value 1000 times a day, it should be ok )
This way you sync the value of the HA sensor with the global variable:
sensor:
- platform: homeassistant
name: "Time delay"
id: ha_delay
entity_id: input_number.failsafe_delay #The entity of input_number in HA
on_value:
- globals.set:
id: failsafe_delay_int
value: !lambda "return x;"
Then you can read the value of the global variable simply in lambda by just calling id(failsafe_delay_int)
in the failsafe script delay.
script:
- id: failsafe
then:
- delay: !lambda "return id(failsafe_delay_int);"
- switch.turn_off: pin0
This would make sure there is an initial value for the delay, and it will be set to correct HA value when HA connects or on each change of the value. But I think this is too complicated and the first example with publish_state should work aswell.
Anyway if you don’t have any other way to activate the switches then through the HA, this is not needed at all, if you set the restore_mode to ALWAYS_OFF, you won’t be able to turn on the switches anyway in case the HA is down.