Changing default state in automation

I have several broadlink switches that are setup to turn on and off but home assistant has no way of knowing their state.

so for example, I have a reptile heat lamp that is automated by sunrise/sunset which works fine, however when home assistant boots, its default state is set to off although the lamp is on.

Is there a way to write into the configuration the default state on boot? This way I can write an automation to set the ‘assumed state’ based on the same rules that turns it on and off?

1 Like

You seem to be asking 2 different questions.
I don’t think there is a way to write default state to the configuration.
I’m not sure how that would be helpful anyway, because you still wouldn’t know if it is off or on.

You can however run an automation on startup. Then you could use your rules to “turn on” or “turn off” a switch that is already on or off.
Depending upon how your switch handles a turn-on message when it’s already on, this may or may not be a problem.

Here’s how you run an automation at startup:

  - alias: 'handle startup event'
    trigger:
      platform: event
      event_type: homeassistant_start

It looks like it’s possible to set the state without calling the turn-on or turn-off service using app-daemon.
See this thread for details: https://community.home-assistant.io/t/set-state-without-communicating-with-device/9217/5

Hi Treno,

Thank you for your response. The solution of turning it on at boot (or off) would do the trick. Can’t believe I didn’t think of that! I was obviously overthinking things.

In regards to writing the default state not being possible. This would surprise me if its not possible at all as you can change states from the states page in developer tools without changing the physical state of a switch.

regards

Stuart

Please write down what you did steps by steps, so other people can benefits on this.

Hi DkAutomater,

I didn’t find a way to execute my original query, but rather use the workaround that treno suggested. I’m unsure what it is you want me to write down?

Its sound that you got the problem fixed, so i was hoping you could write down the solution, for other people.

Not for me, but for other that have the same issue.

/DKAutomater.

Hi,

I didn’t get the problem fixed though? I just confirmed that Treno’s suggestion would work. Treno has already documented a configuration example.

Hmmmmm. I have to admit to being a little confused as to what it is you’re hoping to do. I also have some broadlink switches I use that do not have separate on/off commands so I can’t technically know the state, only assume it. For example, my TV has only one command for on and off, so when I ask alexa to turn on the TV, HA assumes it’s already off so it sends the on/off command, as well as the on command for my receiver. But when I leave the house I only have the receiver turn off (the TV will turn itself off 5 minutes later if it is on), but I don’t send the on/off command for the TV.

Basically is there any state you can tie the lamp state to? To safely assume it’s own state? If you automate the lamp and never manually affect it’s state aside from automations you should be able to come up with a way to turn it on/off and assume you know when it’s on/off.

You could also maybe create an input_boolean and tie that to a custom switch? like so:

  - platform: template
    switches:
      reptile_lamp:
        value_template: "{{ is_state('input_boolean.reptile_lamp', 'on') }}"
        turn_on:
          - service: input_boolean.turn_on
            entity_id: input_boolean.reptile_lamp
          - service: broalink_command
        turn_off:
          - service: input_boolean.turn_off
            entity_id: input_boolean.reptile_lamp
          - service: broalink_command

The boolean should I believe keep it’s state across restarts so this should in theory work? As long as you don’t manually send the broadlink command that is, as that would break the link between the actual state and the input_boolean state.