Automation with if/else

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 }}"
3 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.

Wonder if perhaps you can see why this doesn’t fire?

Automation works when the or condition isn’t there:

condition:
    - "{{ is_state('input_boolean.night_mode', 'off') }}"
    - condition: or
      conditions:
      - "{{ trigger.to_state.state|float < states('input_number.bedroom_temp_min')|float }}"
      - "{{ trigger.to_state.state|float >= states('input_number.bedroom_temp_max')|float }}"

Does it pass Configuration > Server Controls > Check Configuration?

Yup, and no errors in the logs either.

input_number.bedroom_temp_min is 17
input_number.bedroom_temp_max is 24

Both bedrooms ar 24.x and even if I manually set the state higher does do anything. Says the automation has run, but no notification.

Without the or condition it fires fine in either scenario.

Thanks for your help on this :slight_smile:

Post the entire automation that uses this condition.

Here you go, thanks

- alias: 'Bedroom Temps'
  mode: single
  trigger:
    - platform: state
      entity_id:
        - sensor.aarlo_temperature_large_bedroom
        - sensor.aarlo_temperature_small_bedroom
  condition:
    - "{{ is_state('input_boolean.night_mode', 'off') }}"
    - condition: or
      conditions:
      - "{{ trigger.to_state.state|float < states('input_number.bedroom_temp_min')|float }}"
      - "{{ trigger.to_state.state|float >= states('input_number.bedroom_temp_max')|float }}"
  action:
    - service: script.notification
      data:
        notify_service: notify.user_mobile_devices
        title: "Environment Alert"
        subject: "Temperature"
        importance: low
        channel: Environment
        message: "It is {{ 'cold' if trigger.to_state.state|float < states('input_number.bedroom_temp_min')|float else 'hot' }} in {{ trigger.to_state.object_id[18:-8] | title }} Bedroom. Currently it is {{ trigger.to_state.state|float }} °C"

Try this version:

- alias: 'Bedroom Temps'
  mode: single
  trigger:
    - platform: state
      entity_id:
        - sensor.aarlo_temperature_large_bedroom
        - sensor.aarlo_temperature_small_bedroom
  action:
    - variables:
        temp: '{{ trigger.to_state.state|float }}'
        low: "{{ temp < states('input_number.bedroom_temp_min')|float }}"
        high: "{{ temp >= states('input_number.bedroom_temp_max')|float }}"
    - condition: template
      value_template: "{{ is_state('input_boolean.night_mode', 'off') and (low or high) }}"
    - service: script.notification
      data:
        notify_service: notify.user_mobile_devices
        title: "Environment Alert"
        subject: "Temperature"
        importance: low
        channel: Environment
        message: "It is {{ 'cold' if low else 'hot' }} in {{ trigger.to_state.object_id[18:-8] | title }} Bedroom. Currently it is {{ temp }} °C"

If it fails to call script.notification then we can insert a call to persistent_notification.create (shown below) to report the values of the variables to help determine what may be wrong.

Diagnostic version
- alias: 'Bedroom Temps'
  mode: single
  trigger:
    - platform: state
      entity_id:
        - sensor.aarlo_temperature_large_bedroom
        - sensor.aarlo_temperature_small_bedroom
  action:
    - variables:
        temp: '{{ trigger.to_state.state|float }}'
        low: "{{ temp < states('input_number.bedroom_temp_min')|float }}"
        high: "{{ temp >= states('input_number.bedroom_temp_max')|float }}"
    - service: persistent_notification.create
      data:
         title: "{{ now().timestamp() | timestamp_local }}"
         message: "{{temp}} °C, Low = {{'yes' if low else 'no'}}, High = {{'yes' if high else 'no'}}"
    - condition: template
      value_template: "{{ is_state('input_boolean.night_mode', 'off') and (low or high) }}"
    - service: script.notification
      data:
        notify_service: notify.user_mobile_devices
        title: "Environment Alert"
        subject: "Temperature"
        importance: low
        channel: Environment
        message: "It is {{ 'cold' if low else 'hot' }} in {{ trigger.to_state.object_id[18:-8] | title }} Bedroom. Currently it is {{ temp }} °C"

@123 looks like my version worked. Did a full restart for something else and it just started working, though I had done a restart previously.
Perhaps the input_number didnt take properly.

Thanks for your time though, very much appreciated.

1 Like