More elegant way for triggering switch with an input_boolean?

Hi.

I’ve started to setup my home automation with home assistant and I’m amazed by the maturity of the project and the available integrations. Great job!

For my question:

I’m changing via some buttons the state of an input_boolean (via REST-call) and have an automation working that turns a switch on and off, according to the state of this input_boolean:

- id: itb100_to_sonoff_1
  alias: "IT-B-100 to sonoff mapper on"
  initial_state: on
  trigger:
  - platform: state
    entity_id: input_boolean.intertechno_button_it_b_100_left
    to: 'on'
  action:
    - service: switch.turn_on
      entity_id: switch.sonoff

    
- id: itb100_to_sonoff_2
  alias: "IT-B-100 to sonoff mapper off"
  initial_state: on
  trigger:
  - platform: state
    entity_id: input_boolean.intertechno_button_it_b_100_left
    to: 'off'
  action:
    - service: switch.turn_off
      entity_id: switch.sonoff
      

The reason I’m doing it a bit complicated is to have the ability to configure the whole house this way through HA - which is why I’m using it.

The code is working great - but I was wondering, if there isn’t a way to do the same with less code - meaning, if I can’t take the state of the ‘input_boolean’ as the new value of the switch, as this would result in about half the code… .

- id: itb100_to_sonoff_1
  alias: "IT-B-100 to sonoff mapper on"
  initial_state: on
  trigger:
    platform: state
    entity_id: input_boolean.intertechno_button_it_b_100_left
  action:
    service_template: "switch.turn_{{ states('input_boolean.intertechno_button_it_b_100_left') }}" 
    entity_id: switch.sonoff

Trigger on all state changes, use service_template to decide which service to call based on the state of the boolean.

2 Likes

Thanks a lot @anon43302295 for the help - this works great! I didn’t think of the option to use a service_template. :+1:

1 Like