Need help programming a device online/offline check for automation

Hi guys,

I really hope you can help me, as I’m totally lost when it comes to programming of code :cold_face:

I would like to automate this:


  • bathroom light behind a smart switch → controllable with power on/off dumb switch
  • mirror light behind a smart switch → power always on
  • when the dumb switch is toggled OFF the bathroom light AND mirror light should turn OFF (and vice versa)

I can’t define a simple if-this-then-that automation, because I can’t specify a “device unavailable” status :unamused:
So the idea is to create some “ping” to check if the smart switch of the bathroom light is online/offline and create the automation on this.

How do I do that? :smiling_face_with_tear:

My configuration file looks like this:

# Loads default set of integrations. Do not remove.

# Load frontend themes from the themes folder
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

The bathroom light smart switch (IP is called:


The mirror light smart switch (IP is called


Why is the switch unavailable?

What do you mean by manual switch?
A switch that simply cuts power to the smart switch, or a switch that manually controls the smart switch functionally?

There is a power cable for the mirror lighting, but it is not connected to a switch. It can only be switched off by cutting the power (at the fuse). Not a good design in my opinion, but as I’m tenant in the apartment, I’m not allowed to change anything in regard to the wiring.

Exactly, a switch that cuts power to the smart switch:


I changed the wording to ‘dumb switch’

You need to put a smart switch in there, and wire the smart device it powers to always be on. Then you can make your switching work like you want to. Smart switches controlled by cutting the power of to them are a bad idea.

That was my first thought too, but I’m not allowed to replace any switches or wiring. The best I can do is to add those smart switches, which I can remove before I move out of the apartment.

Then just leave that switch on all the time (tape it on with clear tape) and add a battery button right there stuck on the wall to do the switching.
Then that will control all the lights. Most of these have at least single-double-long press, so you could control the lights as a group or individually with the same switch.

Just don’t turn off power to the device and you are good.

I don’t really understand how you are incorporating a smart relay into the circuit in a way that isn’t changing “anything in regard to the wiring” at least as much as putting it in the wall switch, but if that is the case I agree with SG that you should find a way to keep the wall switch in the on position and add a button or other smart input device.

Samotech cover

If that is not possible you can try something like the following:

  - platform: state
    entity_id: switch.bathroom_1
condition: []
  - if:
      - condition: state
        entity_id: switch.bathroom_1
        state: 'on'
      - service: switch.turn_on
        entity_id: switch.bathroom_mirror
      - service: switch.turn_off
        entity_id: switch.bathroom_mirror

Keep in mind that the automation above is very basic and will work best for cases when when both switches start from similar states. It will likely need tuning based on exactly how you want it to handle cases other than what to do when they start from dissimilar states.

I put this code (with replaced entity_ids) to configuration.yaml and it doesn’t work unfortunately.
When i put it to automations.yaml I get this error:

I’m using core-2024.1.5 on a QNAP NAS.

Did you give it an id, alias, and description and use your entity IDs?

If you did those, post your automations.yaml file or, at a minimum, a screen shot.