How to condense these scripts/automations?

Hi,

Is there anyway to dondence these into a single script/automation?

script1:
  alias: Living Room
  sequence:
    - service: switch.turn_on
      entity_id: switch.sonoff04
    - delay: '{{ states.input_number.hours.state | int }}:00'
    - service: switch.turn_off
      entity_id: switch.sonoff04

script2:
  alias: Man Cave
  sequence:
    - service: switch.turn_on
      entity_id: switch.sonoff03
    - delay: '{{ states.input_number.hours.state | int }}:00'
    - service: switch.turn_off
      entity_id: switch.sonoff03

script3:
  alias: Bedroom
  sequence:
    - service: switch.turn_on
      entity_id: switch.sonoff01
    - delay: '{{ states.input_number.hours.state | int }}:00'
    - service: switch.turn_off
      entity_id: switch.sonoff01
- id: 1xxgsfgh
  alias: Turns off LR timer
  trigger:
    platform: state
    entity_id: switch.sonoff04
    from: 'on'
    to: 'off'
  action:
  - service: homeassistant.turn_off
    entity_id: script.script1
  - service: input_select.select_option
    data:
      entity_id: input_select.radiator
      option: None

- id: 1xdfst
  alias: Turns off MC timer
  trigger:
    platform: state
    entity_id: switch.sonoff03
    from: 'on'
    to: 'off'
  action:
  - service: homeassistant.turn_off
    entity_id: script.script2
  - service: input_select.select_option
    data:
      entity_id: input_select.radiator
      option: None

- id: 111xrethn
  alias: Turns off Bedroom timer
  trigger:
    platform: state
    entity_id: switch.sonoff01
    from: 'on'
    to: 'off'
  action:
  - service: homeassistant.turn_off
    entity_id: script.script3
  - service: input_select.select_option
    data:
      entity_id: input_select.radiator
      option: None

Use 1 automation in conjunction with a script that accepts variables.

Script:

variable_script:
  alias: Any room
  sequence:
    - service: switch.turn_on
      data_template:
        entity_id: {{ my_variable_entity_id }}
    - delay: '{{ states.input_number.hours.state | int }}:00'
    - service: switch.turn_off
      data_template:
        entity_id: {{ my_variable_entity_id }}

Automation:

 id: 1xxgsfgh
  alias: Turns off LR timer
  trigger:
    platform: state
    entity_id: 
      - switch.sonoff04
      - switch.sonoff01
      - switch.sonoff03
    from: 'on'
    to: 'off'
  action:
  - service: homeassistant.turn_off
    data_template:
      entity_id: script.variable_script
      my_variable_entity_id: "{{ trigger.entity_id }}"
  - service: input_select.select_option
    data:
      entity_id: input_select.radiator
      option: None

This uses triggers for templates, read up on them here:

1 Like

Or, for ultimate condensement, incorporate the script as the action of the automation…

- id: 1xxgsfgh
  alias: Turns off LR timer
  trigger:
    platform: state
    entity_id: 
      - switch.sonoff04
      - switch.sonoff01
      - switch.sonoff03
    from: 'on'
    to: 'off'
  action:
    - service: switch.turn_on
      data_template:
        entity_id: {{ trigger.entity_id }}
    - delay: '{{ states.input_number.hours.state | int }}:00'
    - service: switch.turn_off
      data_template:
        entity_id: {{ trigger.entity_id }} 
    - service: input_select.select_option
      data:
        entity_id: input_select.radiator
        option: None
1 Like

aww thank you so much. I don’t really understand variable trigger thing but i’m going to give it a try.

Out of interest, why does this not work?

I get this error

Invalid config for [script]: Entity ID switch.{{states.input_select.radiator.state}} is an invalid entity id for dictionary value @ data['script']['script10']['sequence'][0]['entity_id']. Got 'switch.{{states.input_select.radiator.state}}'. (See /config/configuration.yaml, line 30). Please check the docs at https://home-assistant.io/components/script/

# Input Select
  radiator:
    name: Radiator
    options:
     - None
     - Living_Room
     - Man_Cave
     - Bedroom
     - All
    initial: None
    icon: mdi:radiator

# Radiator - Input Select
  - alias: Radiator - Input Select
    trigger:
      - platform: state
        entity_id: input_select.radiator
        to: Bedroom
      - platform: state
        entity_id: input_select.radiator
        to: Man_Cave
      - platform: state
        entity_id: input_select.radiator
        to: Living_Room
    action:
      - service: switch.turn_on
        entity_id: script.script10

# Script
script10:    
  alias: 'Radiator - Input Select'
  sequence:
    - service: switch.turn_on
      entity_id: 'switch.{{states.input_select.radiator.state}}'
    - delay: '{{ states.input_number.hours.state | int }}:00'
    - service: switch.turn_off
      entity_id: 'switch.{{states.input_select.radiator.state}}'

This returns a yaml error

Error loading /config/configuration.yaml: invalid key: “OrderedDict([(‘trigger.entity_id’, None)])”
in “/config/automations.yaml”, line 14, column 0

1 Like

this gave the same error so i assume that i have done something wrong.

Is it to do with my_variable_entity_id? Where do you define this?

Thanks

because you have templates outside template sections in your script, and your input select selections are uppercase. All entity id’s are lowercase, you need to use the lower filter as well.

script10:    
  alias: 'Radiator - Input Select'
  sequence:
    - service: switch.turn_on
      entity_id: 'switch.{{states.input_select.radiator.state}}'
    - delay: '{{ states.input_number.hours.state | int }}:00'
    - service: switch.turn_off
      entity_id: 'switch.{{states.input_select.radiator.state}}'

should look like this:

script10:    
  alias: 'Radiator - Input Select'
  sequence:
    - service: switch.turn_on
      data_template:
        entity_id: 'switch.{{ states.input_select.radiator.state | lower }}'
    - delay: '{{ states.input_number.hours.state | int }}:00'
    - service: switch.turn_off
      data_template:
        entity_id: 'switch.{{ states.input_select.radiator.state | lower }}'

That’s because he made a minor typo, the code in the entity_id sections need quotes:

- id: 1xxgsfgh
  alias: Turns off LR timer
  trigger:
    platform: state
    entity_id: 
      - switch.sonoff04
      - switch.sonoff01
      - switch.sonoff03
    from: 'on'
    to: 'off'
  action:
    - service: switch.turn_on
      data_template:
        entity_id: "{{ trigger.entity_id }}"
    - delay: '{{ states.input_number.hours.state | int }}:00'
    - service: switch.turn_off
      data_template:
        entity_id: "{{ trigger.entity_id }}"
    - service: input_select.select_option
      data:
        entity_id: input_select.radiator
        option: None

It’s defined in the automation if you copied it properly. I wrote it off the top of my head, so there could be errors. Without the errors, I can’t help you debug it.

1 Like

I think I have confused things so if you still don’t mind helping me I want to clear a few things up.

  • I have 4 sonoff switches which I want to control manually to turn on/off the radiators at any time.
  • I want an Input_Select which has a drop-down menu of my switches and when an individual switch is selected I want it to turn on that switch for the period of time which is decided by the input_number slider.
  • I want to also be able to cancel the "Hours/Timer" period if I manually turn off the switch.
  • I also want the input select to revert to "None" once a specific radiator has been selected.

image

I had it all set up before but it involved something like 6 automations and 3 scripts so i was hoping it could be down with fewer things and i could also learn from this in the future.

I would really appreciate if you were able to help.

Thanks

  radiators:
    name: Radiators
    entities:
      - switch.bedroom
      - switch.living_room
      - switch.man_cave
      - switch.switch3
      - input_number.hours
      - input_select.radiator

So you don’t want the switches to only turn the radiators on for the interval? You only want the dropdown to have this functionality?

yes that’s it!

So when its really cold I can turn them on for the night. But, if its a little bit cold i will use the timer to put them on for 1 hour.

what are your radiator selections in the input_select?

  radiator:
    name: Radiator
    options:
     - None
     - Living Room  
     - Man Cave
     - Bedroom
     - Hall
     - All
    initial: None
    icon: mdi:radiator
  1. Providing a solution so that “All” works is extremely complicated with just Yaml. It’s possible but it would require a loop and a counter. It ends up being a ton of work and I don’t have a python script handy at the moment that solves that issue.

  2. This solution will only work for 1 switch at a time. Meaning you cannot have multiple heaters on a timer on at a time. If you want that, then you cannot merry them into a single script. You have to have a separate script for each one. You only have 1 script. If the script is running and you use the dropdown again, the currently running script may get canceled. I haven’t tested it out, it may behave differently.

  3. Probably want to split the automation up again because having it combined means you’ll have to have a bunch of data templates.

split method:

automation:

- id: 1xxgsfgh
  alias: Turns off LR timer
  trigger:
    platform: state
    entity_id: input_select.radiator
  condition:
    condition: template
    value_template: "{{ trigger.to_state.state in [ 'Living Room','Man Cave','Bedroom','Hall'] }}"
  action:
    - service: script.timed_radiator
      data_template: 
        radiator: >
          {% set radiators = {
            'Living Room':'switch.living_room',
            'Man Cave':'switch.man_cave',
            'Bedroom':'switch.bedroom',
            'Hall':'switch.switch3' } %}
          {{ radiators[trigger.to_state.state]] }}

script:

script:
  timed_radiator:
    sequence:
    - service: switch.turn_on
      data_template:
        entity_id: "{{ radiator }}"
    - delay: '{{ states.input_number.hours.state | int }}:00'
    - service: switch.turn_off
      data_template:
        entity_id: "{{ radiator }}"
    - service: input_select.select_option
      data:
        entity_id: input_select.radiator
        option: None

combined method:

- id: 1xxgsfgh
  alias: Turns off LR timer
  trigger:
    platform: state
    entity_id: input_select.radiator
  condition:
    condition: template
    value_template: "{{ trigger.to_state.state in [ 'Living Room','Man Cave','Bedroom','Hall'] }}"
  action:
    - service: switch.turn_on
      data_template: 
        entity_id: >
          {% set radiators = {
            'Living Room':'switch.living_room',
            'Man Cave':'switch.man_cave',
            'Bedroom':'switch.bedroom',
            'Hall':'switch.switch3' } %}
          {{ radiators[trigger.to_state.state]] }}
    - delay: '{{ states.input_number.hours.state | int }}:00'
    - service: switch.turn_off
      data_template:
        entity_id: >
          {% set radiators = {
            'Living Room':'switch.living_room',
            'Man Cave':'switch.man_cave',
            'Bedroom':'switch.bedroom',
            'Hall':'switch.switch3' } %}
          {{ radiators[trigger.to_state.state]] }}
    - service: input_select.select_option
      data:
        entity_id: input_select.radiator
        option: None

Thank you sooo much! It works great.

1 Like

please let me ask this related script variable passing:

when this condition passes:

  - condition: template
    value_template: >
      {{trigger.to_state in ['group.daughter1', 'group.daughter2', 'group.daughter3',
                            'group.daughter4', 'group.wife'] }}

id like to have an announcement be made:

service_template: >
  script.announce_{{'daughters'  if trigger.to_state in ['group.daughter1', 'group.daughter2', 'group.daughter3',
                        'group.daughter4'] else 'wife'}}
    data_template: 
      variables: >
        presence_person: "{{trigger.to_state}}" # or trigger.entity_id?
        presence_location: "{{trigger.to_state.state}}"

script.announce_daughters:
  sequence:
    service: notify.ios_telefoonmhb
    data_template:
      title: '{{presence_person}} arrived home speech'
      message: >-
        {{as_timestamp(now()) | timestamp_custom("%X") }} :
        {{ person}} arrived {{presence_location}}.
      data:
        push:
          sound: US-EN-Morgan-Freeman-Daughter-Is-Arriving.wav

  script.announce_wife:
    sequence:
      service: notify.ios_telefoonmhb
      data:
     title: 'Wife Arrival notification'
        message: 'Your wife is arriving' #{{ trigger.zone.attributes.friendly_name }}
        data:
          push:
            sound: 'US-EN-Morgan-Freeman-Wife-Is-Arriving.wav'

now where do I create the variable using the trigger.to_state and how to place it in the data_template of the script daughter (no variable needed in the wife script, only one there :wink: )

@petro @pnbruckner would you please have a look, I am a bit stuck here… one of my daughters came home, and it selected the wife script… (apparently the condition isn’t correct) and it didnt pass the variables either.

TL; DR. But first, this doesn’t make any sense:

  - condition: template
    value_template: >
      {{trigger.to_state in ['group.daughter1', 'group.daughter2', 'group.daughter3',
                            'group.daughter4', 'group.wife'] }}

trigger.to_state is a state object, not a state string. It will not match anything in a list of strings. I’m guessing you meant trigger.to_state.state, but that’s not clear since you don’t show the trigger.

Second, when you call a script using its name as the service, you don’t put variables under variables:. You just list them directly under data: or data_template:. This is clearly explained here.

No, I meant trigger.to_state, and my trigger is:

  - alias: Presence Tracking
    id: 'Presence Tracking'
    trigger:
      platform: state
      entity_id:
        - group.daughter1
        - group.daughter2
        - group.daughter3
        - group.daughter4
        - group.wife
        - group.me

I want the automation to create to variables, presence_person (trigger.to_state) and presence_location (trigger.to_state.state), and pass these to the scripts.

tbh it isn’t as clear as you say, since I need the triggers to be passed? If you point me to the 'variables: ’ error, you’re right, I’ve taken that out.

seems to work now with this:

  - condition: template
    value_template: >
      {{trigger.entity_id in ['group.daughter1', 'group.daughter2', 'daughter3',
                            'group.daughter4', 'group.wife'] and 
                            trigger.to_state.state == 'home'}}
  - service_template: >
      script.announce_{{ 'daughters' if trigger.entity_id in ['group.daughter1', 'group.daughter2', 'group.daughter3',
                                  'group.daughter4'] else 'wife'}}
    data_template: 
      presence_person: "{{trigger.entity_id.name}}"
      presence_location: "{{trigger.to_state.state}}"

and the 2 scripts:

  announce_daughters:
    sequence:
      service: notify.ios_telefoonmhb
      data_template:
        title: >
          {{presence_person}} arrived home speech
        message: >
          {{-as_timestamp(now()) | timestamp_custom("%X") }} :
          {{- presence_person}} arrived {{presence_location}}.
        data:
          push:
            sound: >
              US-EN-Morgan-Freeman-Daughter-Is-Arriving.wav

  announce_wife:
    sequence:
      service: notify.ios_telefoonmhb
      data_template:
     #        title: 'Wife Arrival notification'
        message: 'Your {{ presence_person}} is arriving {{presence_location}}' #{{ trigger.zone.attributes.friendly_name }}
        data:
          push:
            sound: >
              US-EN-Morgan-Freeman-Wife-Is-Arriving.wav