Button with state uses a switch and an binary sensor

Hi all,
I have several years HA on a pi thats communicates with my beckhoff PLC (with ADS) thats controls my home lights en etc.
For now I used the standaard switches and lights. (lovelace didn’t exist on that time)
I bought a nuc and put proxmox on it and also HA.
en now I wanna upgrade some things.

But first some I gonna explane the situation how I work now.
My beckhoff controls everything. so if I a wanna turn on a light it just needs a pulse. But HA doesn’t work like that. what I do is use a switch in HA , when I turn it ON in HA the PLC receives that ON signal use it and put the switch back to OFF.
that worked for me fine.

what i want to achieve is the following:
I wanna see of my light is on or not in reality.
Is het posible to make a button thats uses the switch to turn a light on and off and colors the icon when it is realy on with a binary sensor comming from my bechoff.

I already read a lot but, the more I read the less I see the light :grinning:
I don’t know were to start, and maybe there is other way thant those switches.

thanks in advance and sorry for my bad english

Hi Jimmy,

It might be a different command based on with twincat you are using. I am using twincat 2 and I have managed to integrate all the lights and enocean wall switches to my Home Assistant.

#Sauna 24
- platform: ads
  adsvar: .arrLampCommands[24].bOn
  name: Sauna on

- platform: ads
  adsvar: .arrLampCommands[24].bOff
  name: Sauna off

- platform: ads
  adsvar: .arrLampProcessOutputData[24].bData
  name: Sauna status

- platform: template
  lights:
    sauna:
      friendly_name: "Sauna"
      value_template: "{{ is_state('light.sauna_status', 'on') }}"
      turn_on:
        service: light.toggle
        target:
          entity_id: light.sauna_on
      turn_off:
        service: light.toggle
        target:
          entity_id: light.sauna_off

This is my code from my light.yaml. If I understand your wishes correctly, you want to see the status of the light but also want to control that also?
In my example light template creates a new entity called light.sauna that can be controlled in Home Assistant.

Hi,

I just figured out an easier way for this and couldn’t really find any clear examples of this. I used this for my coffee machine.

My switch is: switch.power_button
My sensor is: binary_sensor.power_status

Using the template platform, it combines the switch and sensor so the standard button can get the ‘value’ from the binary sensor through the entity “coffee_machine”

here is my code that goes into configuration.yaml:

switch:        
  - platform: template
    switches:
      coffee_machine:
        value_template: "{{ is_state('binary_sensor.power_status', 'on') }}"
        friendly_name: Breville Coffee Machine
        turn_on:
          service: switch.toggle
          target:
            entity_id: switch.power_button
        turn_off:
          service: switch.toggle
          target:
            entity_id: switch.power_button

then just create a standard button entity. You can use the visual editor or here is my code:

show_name: true
show_icon: true
type: button
tap_action:
  action: toggle
entity: switch.coffee_machine
name: Breville Coffee Machine
show_state: false
hold_action:
  action: toggle
icon: mdi:coffee

I hope this helps someone!

1 Like