Can't seem to wrap my head around when I need `Scripts`and when `Automations`

Hi everyone,
I am trying to optimize some of my scripts and automations using simple templating. And in some cases not even templating, but basic conditions.
However, I keep running into issues.

So, problem 1. I would like to change the temperature of different thermostats depending on whether or not the bedroom is already being heated.
This approach fails because the “above” is not supported. But at the same time it does not seem allowed to state: >5.
How can I solve this?

automation:
  - alias: "Climate: Off"
    id: climate_off
    mode: single
    trigger:
      - platform: time
        at: "22:00:00"    
    action:
      - if:
          - condition: state
            entity_id: climate.eq3_climate_master_bedroom
            attribute: temperature
            above: 5
        then:
          - service: script.climate_sleep
        else:
          - service: script.climate_off

I then also tried going the integrated approach, i.e. not creating additional scripts when the script service call should work also in automations directly. That way I can remove a source of error (target script name changing).
But, again, it seems that Automations do not allow templating even though Scripts allow precisely this service call.

automation:
  - alias: "Climate: Off"
    id: climate_off
    mode: single
    trigger:
      - platform: time
        at: "22:00:00"    
    action:
      - service: climate.set_hvac_mode
          data:
            hvac_mode: heat
          target:
            entity_id: '{{states.climate|selectattr(''entity_id'', ''search'', ''climate.eq3'')|map(attribute=''entity_id'')|list}}'
        - service: climate.set_temperature
          data:
            temperature: 4.5
          target:
            entity_id: '{{states.climate|selectattr(''entity_id'', ''search'', ''climate.eq3'')|map(attribute=''entity_id'')|list}}'

(Info: I did not add the condition of the bedroom temperature yet because it failed already without that :smiley: )
So templating the entity_id works in scripts but not in Automations. Why?

Would someone in the community please help me out and let me know why none of the above work (even though they look so straight forward) and how I could solve these issues?

Thank you
Alex

states.climate in scripts looks wrong. What. Entity are you targeting?

you need to use numeric_state:

    action:
      - if:
          - condition: numeric_state
            entity_id: climate.eq3_climate_master_bedroom
            attribute: temperature
            above: 5
        then:
          - service: script.climate_sleep
        else:
          - service: script.climate_off

Of course it does.

Scripts are literally nothing more than the actions section of the automation. they use exactly the same rules and syntax. the docs even imply this by linking the scripts docs of you click on the “actions” section of the automation docs.

you didn’t show the automation that you think isn’t allowed so it’s hard to know why it didn’t seem to work.

1 Like

It is like states.sensor. But since it is a climate entity and not a sensor, it is states.climate.

Oh, of course, you are right. The script works when using numeric state. Stupid mistake :frowning:

The automation that is not working is posted in the initial post:

automation:
  - alias: "Climate: Off"
    id: climate_off
    mode: single
    trigger:
      - platform: time
        at: "22:00:00"    
    action:
      - service: climate.set_hvac_mode
          data:
            hvac_mode: heat
          target:
            entity_id: '{{states.climate|selectattr(''entity_id'', ''search'', ''climate.eq3'')|map(attribute=''entity_id'')|list}}'
        - service: climate.set_temperature
          data:
            temperature: 4.5
          target:
            entity_id: '{{states.climate|selectattr(''entity_id'', ''search'', ''climate.eq3'')|map(attribute=''entity_id'')|list}}'

whereas the script approach works

script:
  climate_off:
    alias: 'Climate: Off'
    icon: hass:thermometer-bluetooth
    sequence:
    - service: climate.set_hvac_mode
      data:
        hvac_mode: heat
      target:
        entity_id: '{{states.climate|selectattr(''entity_id'', ''search'', ''climate.eq3'')|map(attribute=''entity_id'')|list}}'
    - service: climate.set_temperature
      data:
        temperature: 4.5
      target:
        entity_id: '{{states.climate|selectattr(''entity_id'', ''search'', ''climate.eq3'')|map(attribute=''entity_id'')|list}}'
    mode: queued

Your indentation is off in the automation. Everything below the first service: line is indented too far… which causes the second service to be treated like a part of the first service and breaks the first service call.

1 Like

You are right. I had corrected it but I missed one indentation neverthrless and that messed it up :frowning: