Hi Marc,
Wanted to thank you again for putting me on the right track! I needed to go through a crash course advanced yaml to understand what you were doing with the & and * signs, but it is all clear now . For info, I used the “light” template instead of the switch because it also has the light bulb indicator.
Now, there may be more people running into the same issue, so let me provide more details on the configuration and the solution. Hope this will help other people too.
Configuration
- This is about an existing house where lights are steered with impulse switches (aka teleruptors). This means that the lights can also be switched on/off manually. The Home Assistant domotics layer is installed on a Raspberry Pi and is used as a secondary control.
- To monitor the state of a light I have installed relays over the teleruptor outputs. Each relay switches a 5V output, which is captured with an mcp23017 I/O expander. Feel free to use an optocoupler instead of a relay if you prefer, but in my (240V) case using relays was the better option.
- To control the teleruptor with Home Assistant, I have a second mcp23017 board with a series of smaller relays that can toggle the teleruptors.
Controlling the mcp23017 with Home Assistant
I will use two lights in this example, one for the living room and one for the kitchen. The mcp23017 ICs are read/written every 0.2 seconds which gives a nice response time, while I can still control up to eight I/O expanders with the Raspberry Pi. In this example one mcp23017 board is configured as a binary sensor, and the other as an external switch.
Add the following lines to configuration.yaml.
binary_sensor:
- platform: mcp23017
i2c_address: 0x20
scan_interval: 0.2
pins:
0: sensor_light_livingroom
1: sensor_light_kitchen
switch:
- platform: mcp23017
i2c_address: 0x21
scan_interval: 0.2
pins:
0: relay_light_livingroom
1: relay_light_kitchen
Note that in the above example one board has ID 0 on the I2C connection, and the other has ID 1. For controlling the teleruptors, I will switch the relays for 0.4 seconds as follows. The below YAML lines will provide you with two lights that will do the trick. Add the following lines to configuration.yaml.
light:
- platform: template
lights:
light_living:
friendly_name: "Livingroom Light"
value_template: "{{ is_state('binary_sensor.sensor_light_living', 'on') }}"
turn_on:
- condition: state
entity_id: binary_sensor.sensor_light_living
state: 'off'
- service: switch.turn_on
entity_id: switch.relay_light_livingroom
- delay: '0.4'
- service: switch.turn_off
entity_id: switch.relay_light_livingroom
turn_off:
- condition: state
entity_id: binary_sensor.sensor_light_living
state: 'on'
- service: switch.turn_on
entity_id: switch.relay_light_livingroom
- delay: '0.4'
- service: switch.turn_off
entity_id: switch.relay_light_livingroom
light_kitchen:
friendly_name: "Kitchen Light"
value_template: "{{ is_state('binary_sensor.sensor_light_kitchen', 'on') }}"
turn_on:
- condition: state
entity_id: binary_sensor.sensor_light_kitchen
state: 'off'
- service: switch.turn_on
entity_id: switch.relay_light_kitchen
- delay: '0.4'
- service: switch.turn_off
entity_id: switch.relay_light_kitchen
turn_off:
- condition: state
entity_id: binary_sensor.sensor_light_kitchen
state: 'on'
- service: switch.turn_on
entity_id: switch.relay_light_kitchen
- delay: '0.4'
- service: switch.turn_off
entity_id: switch.relay_light_kitchen
Switching off all lights is then done through a script. Add e.g. the following lines to the scripts.yaml file as.
switch_off_all:
alias: Switch all lights off
mode: single
sequence:
- service: light.turn_off
entity_id: light.light_living
- service: light.turn_off
entity_id: light.light_kitchen
It is also possible to add the lights and script to the Overview as
- entity: light.light_living
- entity: light.light_kitchen
- entity: script.switch_off_all
… and this solves the original question.
Acknowledgement
Kudos go to Marc “mf_social” for helping me with the yaml part.
Additional information
Please mind I2C is not enabled by default in Home Assistant. As a new user I cannot add the link, but you will find more information in the Home Assistant site. I had to redo the configuration twice before it actually worked, but this could be me . Just don’t give up.
I wish you much success in your Home Assistant implementation!