When power is lost/restored to the switch, I would like it not to restore the previous state, but to switch to the “on” state.
I tried the “Power-on behavior” property, for some reason only the value ‘off’ works, but ‘on’ does not.
Will there be any tips? Or maybe write some kind of automation so that when power is restored, it turns on the outlet.
If possible, can you throw off the sample code?
PS: I use a smart switch to turn on the light. When switching to the manual switch, you need the light to turn on, and it restores the past state of “off” and the light does not light up
I wrote this code. But this code doesn’t work for some reason.
This code is triggered only 1 time when restarting Home Assistance
What is my mistake?
# Loads default set of integrations. Do not remove. TEST
default_config:
# Load frontend themes from the themes folder
frontend:
themes: !include_dir_merge_named themes
binary_sensor:
- platform: template
sensors:
power_loss:
friendly_name: "Power loss"
device_class: power
value_template: "{{ is_state('switch.0xa4c138c4856a9f2a', 'unavailable') }}"
automation:
- alias: "Turn on the smart socket after power is restored"
trigger:
- platform: state
entity_id: binary_sensor.power_loss
from: 'on'
to: 'off'
action:
- service: switch.turn_on
entity_id: switch.0xa4c138c4856a9f2a
script: !include scripts.yaml
scene: !include scenes.yaml
Try something like this, you don’t need a binary sensor that is just complicating things. And this can be easily created in the gui.
description: "any name you want"
mode: single
trigger:
- platform: state
entity_id:
- your switch
from: unavailable
condition: []
action:
- service: switch.turn_on
data: {}
target:
entity_id: your switch
I figured out the problem of the switch.
The ‘Power-on behavior’ status switches to the ‘on’ state only if the power supply to the device has not been on for more than 5 seconds. Otherwise it won’t work.
It turns out that the script is not needed here.
Thank you all