Automation to turn a switch on when it becomes 'available'

Hi guys,

I know it looks simple but I couldn’t do it. I need an automation to turn a switch on when it becomes available/connected to HA.

Example: When I connect a Sonoff to the power, it will connect to my home assistant, the device status will change to ‘available’. Then my HA automation will detect that the device status has changed from ‘unavailable’ to ‘available’. Then it will turn on the the switch.

I know I could use the Power on State configurarion but I don’t want to do that. I need it to be done via automation.

Can anyone help me here?

- id: '1604858642794'
  alias: Pump 1
  description: ''
  trigger:
  - platform: state
    entity_id: switch.pump_1
    from: unavailable
    to: available
  condition: []
  action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.pump_1
  mode: single

If you remove this line:

   to: available

then the automation is certain to trigger on any change from unavailable to another state.

I removed the line and still it does not work.

I was investigating a little bit further and there is something strange here. Even if I cut the Sonoff’s power HA (device history) still shows the status as ‘off’ and not as ‘unavailable’. How can I solve that?

It certainly won’t work if the switch’s state is never unavailable.

1 Like

You can have a sensor that pings the sonoff and use that as your trigger.
But it will not be instantaneous, there will be some delay.

That’s a good idea and it will probably work (with a small delay). But wasn’t Hassio supposed to kinda do the same by mnonitoring the device status?

I don’t know. It probably depends on the device.
I have one socket that is currently showing state ‘unavailable’.

1 Like

With the MQTT sensors on my system, they only get state “unavailable” when HA restarts in the period where they don’t get updated. Otherwise they retain their last state. You may be seeing the same here.

1 Like