Make switch status, follow sensor status

Hi, I’m new to home assistant. Coming from openhab and liking it so far.
I’m struggling with the following.

I have a PLC where real switches and lights are connected to.
The PLC is master so everything works even when hass is down.
To control a light I have 2 variables on modbus:

  • Status: actual state of light (on/off) = binary sensor in hass
  • Control: to toggle the state = switch in hass

The switch always has to follow the state of the binary sensor.
I only want to put the switch on the dashboard, and the switch should always be the actual state of the binary sensor.

Example:
Light is off, i switch on light via switch in the wall, the binary sensor changes to on, the switch in hass also has to change to on.

I made an automation for this:

- id: "1630416937680"
  alias: spotjestv
  description: ""
  trigger:
    - platform: state
      entity_id: binary_sensor.wago_sensor5
  condition: []
  action:
    - choose:
        - conditions:
            - condition: state
              entity_id: binary_sensor.wago_sensor5
              state: "off"
          sequence:
            - service: switch.turn_off
              target:
                entity_id: switch.wago_switch1
        - conditions:
            - condition: state
              entity_id: binary_sensor.wago_sensor5
              state: "on"
          sequence:
            - service: switch.turn_on
              target:
                entity_id: switch.wago_switch1
      default: []
  mode: single

It works perfectly, but I have about 50 lights so it’s many lines for something simple.
Is there a way to make this a bit more compact and cleaner?

I also can’t figure out how I can put it all in code, I made this in the UI but would like to put it all in a seperate yaml file.
I looked up this link: Automation YAML - Home Assistant
But I can’t get it to work…
Piece of the example below:

# Example of entry in configuration.yaml
automation my_lights:
  # Turns on lights 1 hour before sunset if people are home
  # and if people get home between 16:00-23:00
  - alias: "Rule 1 Light on in the evening"
    trigger:

It complains that I can’t use my_lights… Can someone point out why?

Thanks for the support already!

This should get your code working, but I think it is not really what you want because it stays in a single file.

# This is very important and I'm not sure if you have this in your configuration.yaml file too.
automation: !include automations.yaml

# Labeled automation block
automation kitchen:
  - trigger:
      - platform: ...

This could be your file structure:
when you’re using automation: !include_dir_list ./automations in your configuration.yaml

config
|--automations
|   |--automation1.yaml
|   |--automation2.yaml
|--configuration.yaml

Another alternative for your automation that is a bit more compact-

trigger:
  - platform: state
    entity_id: binary_sensor.wago_sensor5
action:
  - service: >-
      switch.turn_{{ trigger.to_state.state }}
    target:
      entity_id: switch.wago_switch1

Edit: applied suggestion by Taras - resulting in a more compact automation.

Thanks! This made me able to put the automations in different files, which gives a lot more structure.

Thanks! Worked perfect and much cleaner.

You can replace this:

      switch.turn_{{'on' if trigger.to_state.state == 'on' else 'off' }}

with this:

      switch.turn_{{ trigger.to_state.state}}

The State Trigger is listening to a binary_sensor so the values will be on/off.

1 Like

Do you also have an example of multiple automations in 1 file?
I have been fiddling with it for an hour with name and alias properties but I can’t get it to work.

I now have this:

trigger:
  - platform: state
    entity_id: binary_sensor.wago_sensor5
action:
  - service: >-
      switch.turn_{{ trigger.to_state.state}}
    target:
      entity_id: switch.wago_switch1

But I would like this piece of code multiple times in the same file… Really can’t get it to work…

Also another question, is it possible to make this like a kind of function I could call and just pass parameters?
Like: modbusSwitch:=updateSwitch(modbusSensor) or updateSwitch(modbusSensor,modbusSwitch)?

This will work only if the last character in the binary_sensor’s name corresponds to the last character of the corresponding switch’s name. In other words, if binary_sensor.wago_sensor3 corresponds to switch.wago_switch3.

alias: example 567
trigger:
  - platform: state
    entity_id:
    - binary_sensor.wago_sensor1
    - binary_sensor.wago_sensor2
    - binary_sensor.wago_sensor3
    - binary_sensor.wago_sensor4
    - binary_sensor.wago_sensor5
action:
  - service: "switch.turn_{{ trigger.to_state.state}}"
    target:
      entity_id: "switch.wago_switch{{ trigger.to_state.object_id[-1] }}"

Hello @Robbe1991 , I am currently facing the same problem. Have you found an easy solution or written one yourself? Also, I’m still new to HASS. Did you have to create an automation for each button as well?

Currently, this solution doesn’t work for me as expected. How should I implement this solution?

Thanks a lot, Christoph