Update Input Select based on Switches

Hi folks,

I’m struggling with an automation to update my input select based on switch states.
I’ve been creating an input select to control my ventilation system (Low/Medium/High).
Low is basically default, when I want to go to Medium, I toggle Fibaro switch 1, to go to High, I toggle Fibaro switch 2 and turn of switch 1 if turned on prior to that. This is handled by scripts and I’ve managed to this within one automation.

Both Fibaro switches are also connected to a legacy switch. Everything works but now I would like to update the input select when the system is switched with the legacy switch.

I think I can do this with service templates but how?

service_template: >
  {% if is_state ('input_select.ventilatie' ,'Laag') %} script.wtw_laag {% elif
  is_state ('input_select.ventilatie' , 'Medium') %} script.wtw_medium {% elif
  is_state ('input_select.ventilatie' , 'Hoog') %} script.wtw_hoog {% endif %}

Basically if switch 1 is turned on → input select to Medium and if switch 2 is turned on → input select to High. I can do this with 4 different automations but would like to understand how this works with a single one.

I don’t know if this helps but this template:

service_template: >
  {% if is_state ('input_select.ventilatie' ,'Laag') %} script.wtw_laag {% elif
  is_state ('input_select.ventilatie' , 'Medium') %} script.wtw_medium {% elif
  is_state ('input_select.ventilatie' , 'Hoog') %} script.wtw_hoog {% endif %}

can be replaced by this:

service: "script.wtw_{{ states('input_select.ventilatie') | lower }}"

NOTE
The service_template option was deprecated several versions ago in favor of using the service option (which now supports templates).

If you are using an automation to monitor the input_select then it can be done like this:

- alias: example abc
  trigger:
  - platform: state
    entity_id: input_select.ventilatie
  action:
  - service: "script.wtw_{{ trigger.to_state.state | lower }}"

Thanks for your help!

I’m not following, I’m using the service template to call scripts based on the input select value.

How does the service determine which script to run for each state?

It takes the state and uses it as part of the scripts name

alias: Tuinhuis schakelaar
description: ''
mode: single
trigger:
  - platform: state
    entity_id: switch.switchA
    for: 1
  - platform: state
    entity_id: switch.switchB
    for: 1
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: switch.switchA
            state: 'off'
          - condition: state
            entity_id: switch.switchB
            state: 'off'
        sequence:
          - service: input_select.select_option
            target:
              entity_id: input_select.ventilatie
            data:
              option: 'Laag'
      - conditions:
          - condition: state
            entity_id: switch.switchA
            state: 'on'
          - condition: state
            entity_id: switch.switchB
            state: 'off'
        sequence:
          - service: input_select.select_option
            target:
              entity_id: input_select.ventilatie
            data:
              option: 'Medium'
      - conditions:
          - condition: state
            entity_id: switch.switchA
            state: 'off'
          - condition: state
            entity_id: switch.switchB
            state: 'on'
        sequence:
          - service: input_select.select_option
            target:
              entity_id: input_select.ventilatie
            data:
              option: 'Hoog'
    default: []

The for on the triggers is to not trigger in between going from medium to high.

Petro has explained it and here’s an example.

  • Let’s say the Input Select’s state changes to Hoog.
  • states('input_select.ventilatie') reports Hoog.
  • states('input_select.ventilatie') | lower reports hoog.
  • The result is: service: script.wtw_hoog

Ah that makes sense now! Thanks a lot both Taras and Petro!

This was exactly what I was looking for, just tested and works like a charm! Thanks Timo!

Your first post stated you were looking for a way to do it (call your existing scripts) with a service_template. Have your requirements changed?

So based on your example @septillion I would also be able to create one automation to trigger the ventilation based on humidity measured on one of my sensors right?

Option 1, if humidity is above 75% trigger the input select to High, Option 2, if humidity becomes lower than 65% trigger the input select to Medium etc? Am I correct here?

No maybe I did not explain it correctly, I was able to create the automation using the service_template, however I learned by your post that this was deprecated and should be replaced with service. That is working now.

The original requirement was to change the input select based on switch states. So if switch A is turned on and switch B is turned off, change the input select to Medium. If switch A is turned off and switch B is turned on, change the input select to High.

Both are working now, I’ve changed the automation for the input select to call scripts to your suggestion and that works fine and with the example Timo provided I’ve also managed to create an automation to change the state of the input select.

I would even put everything in a single automation and just dump the scripts. This would remove the need for a delay (the for:) by setting the mode to single.

alias: Ventilatie
description: ''
mode: single
max_exceeded: silent
trigger:
  - platform: state
    entity_id: switch.switchA
    id: switch
  - platform: state
    entity_id: switch.switchB
    id: switch
  - platform: input_select.ventilatie
    id: select
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: select
          - condition: state
            entity_id: input_select.ventilatie
            state: 'Laag'
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.switchA
          - service: switch.turn_off
            target:
              entity_id: switch.switchB
      - conditions:
          - condition: trigger
            id: select
          - condition: state
            entity_id: input_select.ventilatie
            state: 'Medium'
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.switchB
          - service: switch.turn_on
            target:
              entity_id: switch.switchA
      - conditions:
          - condition: trigger
            id: select
          - condition: state
            entity_id: input_select.ventilatie
            state: 'Hoog'
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.switchA
          - service: switch.turn_on
            target:
              entity_id: switch.switchB
      - conditions:
          - condition: trigger
            id: switch
          - condition: state
            entity_id: switch.switchA
            state: 'off'
          - condition: state
            entity_id: switch.switchB
            state: 'off'
        sequence:
          - service: input_select.select_option
            target:
              entity_id: input_select.ventilatie
            data:
              option: 'Laag'
      - conditions:
          - condition: trigger
            id: switch
          - condition: state
            entity_id: switch.switchA
            state: 'on'
          - condition: state
            entity_id: switch.switchB
            state: 'off'
        sequence:
          - service: input_select.select_option
            target:
              entity_id: input_select.ventilatie
            data:
              option: 'Medium'
      - conditions:
          - condition: trigger
            id: switch
          - condition: state
            entity_id: switch.switchA
            state: 'off'
          - condition: state
            entity_id: switch.switchB
            state: 'on'
        sequence:
          - service: input_select.select_option
            target:
              entity_id: input_select.ventilatie
            data:
              option: 'Hoog'
    default: []

And yeah, you could make a separate automation which will just set the input_select to the desired state. Although I would not use fixed humidity for that. On rainy summer day you might run into trouble that way.

PS Medium => Middel if you want to keep it Dutch :wink:

1 Like

Even better! Just implemented this and helps a lot keeping the automations down to a minimum.

You would suggest to use a trigger based on increase in humidity I guess? Instead of a fixed amount?
Would you happen to have an example for this as well? :smile:

I’m fairly new to automations, so trying to find my way while trying to keep the amount down to a minimum.

If you’re interested, there’s a shorter way to do this but if you’re satisfied with what has been presented I won’t waste time posting it.

It does depend on what you try to do. If it’s the common bath room fan then I still like the use of a trend-sensor to trigger the start of “more ventilation”. If you save the current humidity when it triggers then you can stop the ventilation again when you drop below that value again (or maybe that value + few percent).

And what I’ve used for things that have states and you want to mix automatic control and manual control (override) is to make a second input_select with the same options as the states + the option “Automatic”. And you only allow the automatic control to run when it’s set to “Automatic” and just duplicate (with an automation) the other states to the first input_select. That way you have an input_select to use from the frontend (set the operation mode) and an input_select which contains the actual state of the device.

And yeah, with templates you can probably write things shorter but I like the UI friendly approach.

Always interested to learn other options as well. If not for this automation it could always be useful for others.

I’ve solved this problem as follows:

alias: Algemeen Balansventilatie
description: ''
trigger:
  - platform: state
    entity_id: input_select.balansventilatie
    id: balansventilatie_input_select_low
    to: Low
  - platform: state
    entity_id: input_select.balansventilatie
    id: balansventilatie_input_select_medium
    to: Medium
  - platform: state
    entity_id: input_select.balansventilatie
    id: balansventilatie_input_select_high
    to: High
  - platform: state
    id: balansventilatie_status_low
    entity_id: sensor.keuken_switch_balansventilatie
    for:
      hours: 0
      minutes: 0
      seconds: 2
    to: Low
  - platform: state
    id: balansventilatie_status_medium
    entity_id: sensor.keuken_switch_balansventilatie
    for:
      hours: 0
      minutes: 0
      seconds: 2
    to: Medium
  - platform: state
    id: balansventilatie_status_high
    entity_id: sensor.keuken_switch_balansventilatie
    for:
      hours: 0
      minutes: 0
      seconds: 2
    to: High
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: balansventilatie_input_select_low
          - condition: not
            conditions:
              - condition: state
                entity_id: sensor.keuken_switch_balansventilatie
                state: Low
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id:
                - switch.balansventilatie_medium
                - switch.balansventilatie_high
      - conditions:
          - condition: trigger
            id: balansventilatie_input_select_medium
          - condition: not
            conditions:
              - condition: state
                entity_id: sensor.keuken_switch_balansventilatie
                state: Medium
        sequence:
          - choose:
              - conditions:
                  - condition: state
                    entity_id: switch.balansventilatie_high
                    state: 'on'
                sequence:
                  - service: switch.turn_off
                    data: {}
                    target:
                      entity_id: switch.balansventilatie_high
                  - delay:
                      hours: 0
                      minutes: 0
                      seconds: 1
                      milliseconds: 0
                  - service: switch.turn_on
                    data: {}
                    target:
                      entity_id: switch.balansventilatie_medium
            default:
              - service: switch.turn_on
                data: {}
                target:
                  entity_id: switch.balansventilatie_medium
      - conditions:
          - condition: trigger
            id: balansventilatie_input_select_high
          - condition: not
            conditions:
              - condition: state
                entity_id: sensor.keuken_switch_balansventilatie
                state: High
        sequence:
          - choose:
              - conditions:
                  - condition: state
                    entity_id: switch.balansventilatie_medium
                    state: 'on'
                sequence:
                  - service: switch.turn_off
                    data: {}
                    target:
                      entity_id: switch.balansventilatie_medium
                  - delay:
                      hours: 0
                      minutes: 0
                      seconds: 1
                      milliseconds: 0
                  - service: switch.turn_on
                    data: {}
                    target:
                      entity_id: switch.balansventilatie_high
            default:
              - service: switch.turn_on
                data: {}
                target:
                  entity_id: switch.balansventilatie_high
      - conditions:
          - condition: trigger
            id: balansventilatie_status_low
          - condition: not
            conditions:
              - condition: state
                entity_id: input_select.balansventilatie
                state: Low
        sequence:
          - service: input_select.select_option
            data:
              option: Low
            target:
              entity_id: input_select.balansventilatie
      - conditions:
          - condition: trigger
            id: balansventilatie_status_medium
          - condition: not
            conditions:
              - condition: state
                entity_id: input_select.balansventilatie
                state: Medium
        sequence:
          - service: input_select.select_option
            data:
              option: Medium
            target:
              entity_id: input_select.balansventilatie
      - conditions:
          - condition: trigger
            id: balansventilatie_status_high
          - condition: not
            conditions:
              - condition: state
                entity_id: input_select.balansventilatie
                state: High
        sequence:
          - service: input_select.select_option
            data:
              option: High
            target:
              entity_id: input_select.balansventilatie
mode: parallel
max: 10

This has three advantages:

  1. There is an additional safety feature: both switches cannot be on at the same time. Some ventilation systems could become damaged if the two switches are on at the same time. By adding a second delay between “switching the switches”, you prevent any situation in which both would be on for a few miliseconds. I’ve incorporated similar additional safety within my Shelly 2.5 switch as well (independent of home assistant). By having all this in place, you are replicating what the original turning switch is doing:
    image

The physical design of the switch prevents that both switches are on at the same time, because the middle position (Low) is turning both switches off. So you are always first forcing a complete off, before turning anything on.
2. I’ve added an additional condition so that any change in the input select will only try to change the state of the ventilation, if the ventilation state is different as compared to the input select. It’s a small addition, and from functional point of view nothing will change. Just a small improvement so it will check this upfront, instead of afterwards. This also makes debugging easier.
To do this, i’ve created a template sensor in configuration.yaml

sensor:
  - platform: template
      opbergruimte_balansventilatie:
       friendly_name: 'Opbergruimte Balansventilatie'
       unique_id: 598d8eca-6637-47bc-b174-5802f613248f
       value_template: >
          {% if is_state('switch.balansventilatie_medium', 'off') and is_state('switch.balansventilatie_high', 'off') %}
            Low
          {% elif is_state('switch.balansventilatie_medium', 'on') and is_state('switch.balansventilatie_high', 'off') %}
            Medium
          {% elif is_state('switch.balansventilatie_medium', 'off') and is_state('switch.balansventilatie_high', 'on') %}
            High
          {% else %}
            Error
          {% endif %}
  1. Debugging the automation becomes easy, everything is in one overview. This is also why the template sensor becomes handy: more debug info, because you see the current state of the ventilation.