Light states lost after restart

Hello,

I created these templates to switch on/off my lights :

light.yaml

  - platform: template
    lights:
      lights_hallway:
        friendly_name: "Lichten gang"
        turn_on:
          service: homeassistant.turn_on
          entity_id: switch.do_not_use_verlichting_hallway
        turn_off:
          service: homeassistant.turn_off
          entity_id: switch.do_not_use_verlichting_hallway

switch.yaml

  - platform: command_line
    switches:
        do_not_use_verlichting_hallway:
          command_on: 'curl "http://192.168.1.3:80/postEvent.html?action=input&STM=0&MOD=5&CHA=0&EVT=3"'
          command_off: 'curl "http://192.168.1.3:80/postEvent.html?action=input&STM=0&MOD=5&CHA=1&EVT=3"'

This works perfect, except that the state is lost when restarting home assistant (state is off even if light is on).

I could make 2 automations to save and restore the states in combination with helpers, but wonder if there is a better way.

You would need to provide “value_template” to provide the state on restart. Can you read the state from the switch?

Unfortunately not…

For the moment I have this to save the status :

alias: Status verlichting bijhouden
description: ""
trigger:
  - platform: state
    entity_id:
      - light.lights_hallway
    id: gang
condition: []
action:
  - if:
      - condition: template
        value_template: "{{ states(trigger.entity_id) == 'on' }}"
    then:
      - service: input_boolean.turn_on
        target:
          entity_id: input_boolean.toggle_status_lichten_{{ trigger.id }}
    else:
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.toggle_status_lichten_{{ trigger.id }}
mode: queued
max: 10

And this to restore it :

description: ""
mode: single
trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - if:
      - condition: state
        entity_id: input_boolean.toggle_status_lichten_gang
        state: "on"
    then:
      - service: light.turn_on
        data: {}
        target:
          entity_id: light.lights_hallway
    else:
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.lights_hallway

....and all other lights...

This works fine for all the normal lights wich can only be on or off, so when a light is on and I switch it on again nothing changes. But for my dimmer lights I should have the possibility to change the status of the light without actualy switching the light on (cause it is on and don’t know how it is dimmed). Is that possible ?

I’ve needed something similar before, and I used MQTT with retain so that state is restored on restart. Something along the lines of:

light:
  - name: "Dummy light 1"
    schema: json
    state_topic: "mb/ha/config/mqtt.yaml/dummy/1/state"
    command_topic: "mb/ha/config/mqtt.yaml/dummy/1/set"
    brightness: true
    qos: 0
    optimistic: true

and then an automation trigger that handles the command and sends the state topic (although I use Node Red):

automation:
  trigger:
    - platform: mqtt
      topic: "mb/ha/config/mqtt.yaml/dummy/1/set"
  action:
    - service: script.on_off
      data:
        state: {{ value_json }}
    - service: mqtt.publish
      data:
        topic: "mb/ha/config/mqtt.yaml/dummy/1/state"
        payload_template: {{ value_json }}
        retain: true

You could get rid of the extra “on_off” script by having the logic in the automation and using “switch.turn_on/off”. One benefit of this approach is you can have one automation for many lights/switches by using wildcards in the topics and “topic_template” for state (although I’m not sure how you can get the incoming topic in YAML).

This approach would also work with dimmers as you could include the brightness in the MQTT state topic and pull it back out using “brightness_value_template” or any of the other “*_template” options.

Note that I have not tested the above as I’ve converted my Node Red code to YAML equivalent.

Just want to add you can simplify your action to make it less verbose and leverage templating more:

      - service: light.turn_{{ 'on' if is_state('input_boolean.toggle_status_lichten_gang', 'on') else 'off')
        target:
          entity_id: light.lights_hallway
1 Like

need to add a

value_template:

I found this, wich seems to be what I need : REST API | Home Assistant Developer Docs

So I created this :

  turn_lights_hallway_on:
    url: http://192.168.1.6:8123/api/states/light.lights_hallway
    method: POST
    headers:
      authorization: 'Bearer ABC'
      content-type: 'application/json'
    payload: '{"state":"on"}'

I replace ABC by a Long-Lived Access Token.

And replaced in my automation the light.turn_on by this :

      - service: rest_command.turn_lights_hallway_on
        data: {}

But I get an error :

Logger: homeassistant.components.http.ban
Source: components/http/ban.py:84
Integration: HTTP (documentation, issues)
First occurred: 08:37:12 (1 occurrences)
Last logged: 08:37:12

Login attempt or request with invalid authentication from 192.168.1.6 (192.168.1.6). Requested URL: ‘/api/states/light.lights_hallway’. (HomeAssistant/2022.10.5 aiohttp/3.8.1 Python/3.10)

Logger: homeassistant.components.rest_command
Source: components/rest_command/init.py:137
Integration: RESTful Command (documentation, issues)
First occurred: 08:37:12 (1 occurrences)
Last logged: 08:37:12

Error. Url: http://192.168.1.6:8123/api/states/light.lights_hallway. Status code 401. Payload: b’{“state”:“on”}’

What did I wrong ?

Found it : seems I didn’t copy the whole token :crazy_face: