Automation with if/else

Tags: #<Tag:0x00007f326ede5c40>

Any ideas as to what is wrong here with the below.
Automation is triggered by either one of two entities air quality, but want the “Room” value to send to a script to send a notification but HA isn’t liking something here.
Config is saying it is ok, but reloading the automations get an error to check.

Thanks!

- alias: 'Bedroom VOCs'
  mode: single
  trigger:
    - platform: state
      entity_id: sensor.aarlo_air_quality_small_bedroom
    - platform: state
      entity_id: sensor:aarlo_air_quality_large_bedroom
  condition:
    - condition: state
      entity_id: input_boolean.kids_bedtime
      state: true
    - condition: state
      entity_id: input_boolean.night_mode
      state: false
    - condition: numeric_state
      entity_id: "{{ trigger.entity_id }}"
      above: input_number.abnormal_voc
  action:
    - service: script.environment
      data: 
        Room: >
          {% if trigger.to_state.entity_id == aarlo_air_quality_large_bedroom %} Large
          {% else %} Small {% endif %}

The entity_id in the second trigger contains a syntax error. It uses a colon after the word sensor instead of a period.

Valid state values for an input_boolean are 'on' and 'off' as opposed to true/false.

As far as I know, you cannot template the entity_id of a Numeric State Condition.

The action’s template is incorrect. I believe you want this:
trigger.to_state.object_id == 'aarlo_air_quality_large_bedroom'
instead of this:
trigger.to_state.entity_id == aarlo_air_quality_large_bedroom

In fact the template can be reduced to this:

 data: 
   Room: "{{ trigger.to_state.object_id[18:-8] | title }}"

Thanks, huge help.

I’ve almost got it sorted, do you know if there is a way to convert the trigger.to_state to an integer?

I’ve got this:

- condition: template 
      value_template: "{{ trigger.to_state >= states('input_number.abnormal_voc')|int }}"

trigger.to_state is the whole object with entity_id, state etc, so you can’t convert this to an int. I assume you want to convert the state to an int:

- condition: template 
  value_template: "{{ trigger.to_state.state | int >= states('input_number.abnormal_voc')|int }}"

Here is your automation incorporating several features, notably the use of shorthand notation for Template Conditions:

- alias: 'Bedroom VOCs'
  mode: single
  trigger:
    - platform: state
      entity_id:
        - sensor.aarlo_air_quality_small_bedroom
        - sensor.aarlo_air_quality_large_bedroom
  condition:
    - "{{ is_state('input_boolean.kids_bedtime', 'on') }}"
    - "{{ is_state('input_boolean.night_mode', 'off') }}"
    - "{{ trigger.to_state.state|int > states('input_number.abnormal_voc')|int }}"
  action:
    - service: script.environment
      data: 
        Room: "{{ trigger.to_state.object_id[18:-8] | title }}"
2 Likes

That is working perfectly thank you very much.

Didn’t know about the shorthand, will keep that in mind for some of my others.

1 Like

Glad to hear it works now.

Please consider marking my post with the Solution tag. Only one post in the thread can be marked. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has an accepted solution. Effectively, the topic is resolved. This helps users find answers to similar questions.