Automation | Platform Template | Triggers | Not Triggering

Hi everyone,

I am running into a weird issue whilst writing up a new automation:

automation heater_on:
  - id: heater_on
    alias: "Start heater if the temperature in the living room falls below 22C degrees and someone is home"
      - platform: template
        value_template: "{{ states('sensor.motion_sensor_living_room_temperature_measurement') | float < 22 }}"
      - condition: or
          # At least one iPhone needs to be connected to the wifi - i.e. someone is home
          - condition: template
            value_template: "{{ is_state('sensor.iphone_ssid', 'MyInternet') }}"
          - condition: template
            value_template: "{{ is_state('sensor.iphone_2_ssid', 'MyInternet') }}"

      - service: notify.mobile_app_iphone
          title: "Turning fan on"
          message: "Turning on fan as it is now less than 22 degrees in the living room"
      # Turn on the fan
      - service: homeassistant.turn_on

The above automation never gets triggered (I am testing through the Dev Menu > States > Setting the state of the temperature sensor).

After a few different tries, I realised that the issue is with the platform: template trigger. When I replace the trigger with the below as a test, it works perfectly:

- platform: state
  entity_id: sensor.motion_sensor_living_room_temperature_measurement
  from: '14'
  to: '16'

The above works because I manually set the state to 14, and then to 16.

Now you might be wondering why I am using template where I could just do:

- platform: numeric_state
  entity_id: sensor.motion_sensor_living_room_temperature_measurement
  below: 22

The issue is that for some reason, the temperature state variable comes back as a string (verified through the templates debugger menu).

Any help would be greatly appreciated!

States are always strings.

The numeric state trigger should work.

Thanks @tom_l, I did not know that, that is very good to know.

I just tried both of those options again:

- platform: numeric_state
        entity_id: sensor.motion_sensor_living_room_temperature_measurement
        below: 22


- platform: numeric_state
        entity_id: sensor.motion_sensor_living_room_temperature_measurement
        below: "22"

and the automation does not get triggered. Nothing is showing in the logs either…

FYI - in the templates editor in the dev tools, I tried to log out states('sensor.motion_sensor_living_room_temperature_measurement') and it does return the correct temperature.

Did the the temperature change from above 22 to below 22?

Hi Tom, that is a very good point, it was technically already below 22 degrees when I tried my automation again! I just checked.

That would explain it then! I just tried to force the state to 25 degrees, then back to 20, and it worked!

Thanks so much for your help, much appreciated.