Trigger based on temperature change

Hello,

I have a zigbee temperature and humidity sensor paired with homeassistant using zigbee2mqtt.

alias: AC
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ states('sensor.temperature_and_humidity_sensor_temperature') | float > 
      28  }}
    id: "On"
  - platform: template
    value_template: >-
      {{ states('sensor.temperature_and_humidity_sensor_temperature') | float < 
      27  }}
    id: "Off"
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: "On"
        sequence:
          - service: google_assistant_sdk.send_text_command
            data:
              command: Turn on AC
          - service: google_assistant_sdk.send_text_command
            data:
              command: Set AC temp to 16
      - conditions:
          - condition: trigger
            id: "Off"
        sequence:
          - service: google_assistant_sdk.send_text_command
            data:
              command: Turn off AC
mode: single

I am trying to Turn ON the AC when the temperature goes above 28 and if the temperature is below 27 the AC should turn off.

Kindly guide what am I doing wrong in this case as the automation is not triggering

Thanks

What value does that give?
Also keep in mind that this will trigger everytime your sensor sends a value higher then 28.
I think you are better off just using numeric_state that way you can just use the entity without needing a template, as it will only trigger on the threshold.

trigger:
  - platform: numeric_state
    entity_id: sensor.temperature_and_humidity_sensor_temperature
    above: 28
    id: "On"
  - platform: numeric_state
    entity_id: sensor.temperature_and_humidity_sensor_temperature
    below: 27
    id: "Off"

It gives a float value e.g. 31.23

This was my initial automation but it was also not triggering that’s why shifted to template…

Kindly help

Can u do the following:

  1. In the Developer Tools there is a tab called States, find your sensor there and set it to 20. After a while it will update and show the correct value again. That way we were below 27 and again above 28.
  2. Post the trace for the below and above when that happens. U can find that in the automation itself.

No it won’t, it’ll trigger every time the template goes from false to true, effectively working the same as Numeric State:

Agree that Numeric State is better, though.

@Vandana — as @Dujith says, we need to see traces or the lack of them. You say it’s not triggering, but are you sure it’s not the action that is failing to work?

1 Like

Something new learned, my assumption (and yes i know i shouldnt assume :P) was that it would trigger each time a value higher then compared was given. But reading the text i see its not.

Yes I am sure it’s not triggering, because I tried with sending notifications also instead of Google commands.

I saw the traces also and it was not going to any template. I will post the traces once I reach home.

Please do. If you saw traces, then it is triggering.


Please check

The AC was running at the time of screenshot and temperature via sensor was 26.12…

So in this case the automation should have triggered and should have turned off the AC but it didnt

Ah, you’ve executed it manually. In that case, neither trigger fired, and there’s no trigger ID, so your choose doesn’t go either way. It’s working as designed.

What happens if you fake a new value like as @Dujith suggested? Trigger based on temperature change - #4 by Dujith

Ah, you’ve executed it manually. In that case, neither trigger fired, and there’s no trigger ID, so your choose doesn’t go either way. It’s working as designed.

What happens if you fake a new value like as @Dujith suggested? Trigger based on temperature change - #4 by Dujith

I tried with a fake value of 42, the automation triggerred and AC was turned ON. But when the room temp dropped to 26, the automation should have triggerred again to turn off the AC but it didnt…

Didn’t trigger (no trace) or didn’t turn off the AC?

Didn’t trigger…no trace

Have you considered using a threshold helper to provide your trigger?

1 Like

Let me try that

alias: ACC
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.ac_temp
condition: []
action:
  - if:
      - condition: state
        entity_id: binary_sensor.ac_temp
        state: "on"
    then:
      - service: google_assistant_sdk.send_text_command
        data:
          command: TURN ON AC
    else:
      - service: google_assistant_sdk.send_text_command
        data:
          command: TURN OFF AC
mode: single

And this is the threshold helper

Working perfectly…

Thanks a bunch…

ps: still wondering why the template or the numeric method is not working…:thinking::thinking:

This is triggering at every temperature change. How can I avoid that ???

You have a state trigger without specifying what state, so this will trigger on every state change. Two triggers, one on and one off would stop this happening.

I personally would have two automations, one triggering when the helper changes to on and the other when it changes to off. Then you wouldn’t need the conditions.

1 Like

I missed that part, changed it now… working as expected now…

Thank you guys for your help and guidance…