Using arguments from automation in script - specifically state_attr calculation

Hi all,

I’ve seen a lot of threads around passing variables to a script and utilising them (which I’ve done successfully) but what I haven’t managed to do is to use that argument within a template. I simply want to the the state_attr calculation recognise the dynamic entity I’m passing through but it evaluates to None/null. Code below any help greatly appreciated (see: state_attr(states(entity)…)

Automation

service: script.1711691632906
metadata: {}
data:
  entity: >-
    {{
    states('input_select.helper_master_bedroom_bedside_table_switch_entity_select')
    }}
enabled: true

Script


alias: light_brightness_down_test
sequence:
  - service: light.turn_on
    metadata: {}
    data:
      entity_id: "{{ entity }}"
    enabled: false
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{
              state_attr(states('input_select.helper_master_bedroom_bedside_table_switch_entity_select'),'brightness')|int(0)*100//255
              > 5}}
            enabled: false
          - condition: template
            value_template:  >-
              {{
              state_attr(states(entity),'brightness')|int(0)*100//255
              > 5}}
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              brightness_step_pct: -5
            target:
              entity_id: "{{ entity }}"
      - conditions: []
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              brightness_pct: 1
            target:
              entity_id: "{{ entity }}"
mode: single
fields:
  entity:
    selector:
      text: {}
    name: entity
    required: true

What does the trace show?

Have you checked that your light will actually work with brightness_step_pct and brightness_pct ?

1 Like

The trace (in I think the relevant part shows):

conditions/1
Executed: 29 March 2024 at 17:57:26
Result:

result: false
entities:
  - light.master_bedroom
  - 'on'

I didn’t realise these traces showed values - Thanks! The { entity } argument was being resolved but it’s value is effectively the value of: states(‘input_select.helper_master_bedroom_bedside_table_switch_entity_select’) so I needed to simply pass in entity directly into state attr. Stupid mistake thanks for the logical step to diagnose. For anyone stumbling upon this post the revised script code which lowers brightness conditionally with a passed in entity from an automation.

alias: light_brightness_down_test
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ state_attr(entity,'brightness')|int(0)*100//255 > 5}}"
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              brightness_step_pct: -5
            target:
              entity_id: "{{ entity }}"
      - conditions: []
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              brightness_pct: 1
            target:
              entity_id: "{{ entity }}"
mode: single
fields:
  entity:
    selector:
      text: {}
    name: entity
    required: true