Using helpers as variables in automations

Hi there,

First time poster here. I have some coding experience but not much in any of the languages (Python, JSON, YAML) that are relevant for HA. I am trying to write an automation to set my generic thermostat at the right temperature based on different schedules for different weeks. I made an input_select in which I manually indicate what kind of week it is. Currently I programmed that as quoted below (there’s some more checks in that automation but I left that out since it is not relevant here).
It works. But it looks really ugly to me. If I want to add another schedule I have to add extra triggers and extra options for the ‘choose’. I’ve tried creating a variable with the list of schedule entities and one with the list of options from the input_select and replacing the list of trigger entities to {{ schedules }} but that gave an error. How do I
1 Create a variable that automatically contains all options from the input_select
2 Create a variable that contains a mapping of each of these options to a schedule
3 Create an automation that does the following: a) Trigger if any of the listed schedules changes state b) Check which option is selected currently in the input c) Set the preset mode of the generic thermostat according to the state of the schedule

Thanks! Allard

trigger:
  - platform: state
    entity_id:
    - schedule.nr1
    - schedule.nr2
    - schedule.nr3
    from: 'off'
    to: 'on'
    id: onswitch
  - platform: state
    entity_id:
    - schedule.nr1
    - schedule.nr2
    - schedule.nr3
    from: 'on'
    to: 'off'
    enabled: true
    id: offswitch
  action:
  - choose:
    - conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: input_select.what_schedule
          state: number 1
        - condition: state
          entity_id: schedule.nr1
          state: 'on'
        alias: On - 1
      sequence:
      - service: climate.set_preset_mode
        data:
          preset_mode: home
        target:
          entity_id: climate.th1
    - conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: input_select.what_schedule
          state: number 2
        - condition: state
          entity_id: schedule.nr2
          state: 'on'
        alias: On - 2
      sequence:
      - service: climate.set_preset_mode
        data:
          preset_mode: home
        target:
          entity_id: climate.th1
    - conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: input_select.what_schedule
          state: number 1
        - condition: state
          entity_id: schedule.nr1
          state: 'off'
        alias: Off - 1
      sequence:
      - service: climate.set_preset_mode
        data:
          preset_mode: away
        target:
          entity_id: climate.th1
    - conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: input_select.what_schedule
          state: number 2
        - condition: state
          entity_id: schedule.nr2
          state: 'off'
        alias: Off - 2
      sequence:
      - service: climate.set_preset_mode
        data:
          preset_mode: away
        target:
          entity_id: climate.th1
    - conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: schedule.nr3
          state: 'on'
        - condition: state
          entity_id: input_select.what_schedule
          state: number 3
        alias: On - 3
      sequence:
      - service: climate.set_preset_mode
        data:
          preset_mode: home
        target:
          entity_id: climate.th1
    - conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: input_select.what_schedule
          state: number 3
        - condition: state
          entity_id: schedule.nr3
          state: 'off'
        alias: Off - 3
      sequence:
      - service: climate.set_preset_mode
        data:
          preset_mode: away
        target:
          entity_id: climate.th1
    default:
    - stop: No reason to do anything

Is this the actual automation or did you change something for the purpose of sharing it with others?

The reason why I ask is because it seems like a lot of code for what it achieves:

  • When a schedule turns on it sets the thermostat’s preset_mode to home.
  • When a schedule turns off it sets the thermostat’s preset_mode to away.

In other words, it doesn’t seem to matter which schedule is turning on/off, the resulting action is the same (set thermostat’s preset_mode to home/away).

The only differentiator is the input_select which determines which one of the schedules should be controlling the thermostat.

Is that correct?

I changed some things (names of the schedules) but what you describe is indeed what it does. The schedules switch on and off at different times. A more advanced version would be to have schedules with more than two states so I could switch to other states of the thermostat but that would be a different topic I guess?
It is a lot of code for what it achieves indeed. That’s basically the reason for starting this topic. I’m not very experienced in programming automations yet, and I couldn’t find a better way to do it but I would be surprised if that better way doesn’t exist. I would like it to be such that if I define another schedule and its associated value in the input_select, all I would have to do in this automation is add those to values to two variables. It would also be more elegant if it only checked the active schedule for changes but that’s a bit of a chicken and egg problem because what would trigger the automation to check which schedule is active?