Help with automation trigger when sensor temp is higher/lower than weather?

As a general rule I usually figure it out seconds after giving up and asking for help. My problems were I needed value_template in the trigger, not data_template. Don’t really know why but it works. Also needed .state at the end of each entity

Incorrect:
data_template: {{states.sensor.outdoor_temp > states.sensor_bedroom_temperature}}

Correct:
value_template: {{states.sensor.outdoor_temp.state > states.sensor.bedroom_temperature.state}}

But now that I have that working, could someone help adding an additional condition so it only triggers when bedroom_temperature is above 73?

1 Like
- alias: 'Bedroom hotter than outside'
  trigger:
    platform: template
    data_template: {{states.sensor.outdoor_temp.state > states.sensor_bedroom_temp.state}}
  condition:
    condition: numeric_state
    entity_id: sensor.bedroom_temperature
    above: 73
  action:
    service: zwave.set_config_parameter
    data_template: {
      "node_id": 2,
      "parameter": 25,
      "value": "Red"
    }

More examples here:

2 Likes

Also if you don’t actually need the outdoor temperature sensor you made, you should be able to trigger like this:

- alias: 'Bedroom hotter than outside'
  trigger:
    platform: template
    data_template: {{ states.weather.home.attributes.temperature > states.sensor_bedroom_temp.state }}

I recently wrote something like this a month ago when I woke up cold one morning because the AC was on but it was 64 outside. I am using the Yahoo Weather Sensor for the outside temperature.

I know this isn’t exactly what you are looking for but it might give you some clues as to how to accomplish what you need.

- id: hvac_ac_raise_target_temp
  alias: AC- Raise Target Temperature if Cooler Outside
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: sensor.house_thermostat_nest_hvac_state
      from: 'off'
      to: 'cooling'
  condition:
    condition: and
    conditions:
      # If the HVAC is in cool mode
      - condition: state
        entity_id: climate.house_nest
        state: 'cool'
      # If outside temperature + 8 degrees is less than the current house temp
      - condition: template
        value_template: '{{ (states.weather.yweather.attributes.temperature + 8) < states.climate.house_nest.attributes.current_temperature }}'
  action:
    # Notify my phone
    - service: notify.with_email
      data:
        title: 'Triggered: Raise Target Temperature if Cooler Outside'
        message: 'The outside tempature + 8 is: {{(states.weather.yweather.attributes.temperature + 8)}} and Nest current/target temperature is set to {{ (states.climate.house_nest.attributes.current_temperature | int) }}.'
    # If it is colder outside than inside then raise the house target temperature to shutoff the AC
    - service: climate.set_temperature
      data_template:
        entity_id: climate.house_nest
        temperature: '{{ (states.climate.house_nest.attributes.current_temperature | int) }}'
3 Likes
value_template: >
  {{ states.sensor.outdoor_temp.state > states.sensor.bedroom_temperature.state and
     states.sensor.bedroom_temperature.state > 73 }}

Does this actually work for you? States are often always strings, even if they look like numbers. You have to be careful - you can compare strings, and often they will get you expected results, but not always. E.g., “74” > “73” evaluates to true as you’d expect. But “7” > “173” also evaluates to true!

More than likely you need this:

  {{ states.sensor.outdoor_temp.state|float > states.sensor.bedroom_temperature.state|float and
     states.sensor.bedroom_temperature.state|float > 73 }}

And actually I’d recommend using the states() function, like this:

  {{ states('sensor.outdoor_temp')|float > states('sensor.bedroom_temperature')|float and
     states('sensor.bedroom_temperature')|float > 73 }}
2 Likes

Thanks! I realized that after I got it working but now that the outdoor temp shows up as a badge on the UI I kind of like it. It also allows me to click on the badge and see the history like any other sensors so I can easily check how cold it got last night.
Do you know if there’s a way to display the graph as a card on the UI? Possibly with multiple data sets like indoor temp overlaid on outdoor temp?
My ecobee app does all that but I’m really enjoying having everything in one interface.

1 Like

Blockquote
Does this actually work for you? States are often strings, even if they look like numbers. You have to be careful - you can compare strings, and often they will get you expected results, but not always. E.g., “74” > “73” evaluates to true as you’d expect. But “7” > “173” also evaluates to true!
More than likely you need this:

You are probably right, thanks! I actually haven’t seen the expression turn false yet because of the weather, I just assumed it was working because I was getting a true output and changing the comparator direction made it false. I’ll add the float syntax as well as use the states() function.

I have exactly the same automations. Some of it is in Dutch… I have it running for a couple of weeks and with the heat in the Netherlands it is working perfect! I also have a window-sensor, so only get the relevant message :slight_smile:

But I use the difference in temperature for my triggers:

Automations:

- alias: Bericht sturen als buiten kouder dan kamer en binnen >20 graden
  trigger:
    - platform: numeric_state
      entity_id: sensor.delta_temp_kamer
      above: 1
      for: 
        minutes: 10
  condition:
    condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.netatmo_kamer_temperature_2
        above: 20
      - condition: state
        entity_id: binary_sensor.window_sensor_1
        state: 'off'
  action:
    - service: notify.telegram_me
      data_template:
        message: '*Het is nu buiten {{ states.sensor.delta_temp_jasper.state }} graden kouder dan binnen in de kamer waar het {{ states.sensor.netatmo_jasper_kamer_temperature_2.state }} graden is, tijd om het raam open te zetten*'

- alias: Bericht sturen als buiten warmer is dan de kamer en buiten >20 graden
  trigger:
    - platform: numeric_state
      entity_id: sensor.delta_temp_kamer
      below: -0.5
      for: 
        minutes: 10
  condition:
    condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.rotterdam_temperature
        above: 20
      - condition: state
        entity_id: binary_sensor.window_sensor_1
        state: 'on'
  action:
    - service: notify.telegram_me
      data_template:
        message: '*Het is nu buiten {{ (float(states.sensor.delta_temp_jasper.state))*-1 }} graden warmer dan binnen in kamer waar het {{ states.sensor.netatmo_jasper_kamer_temperature_2.state }} graden is, tijd om het raam dicht te doen*'

Template for delta temp:

- platform: template
    sensors:
      delta_temp_kamer:
        value_template: '{{ ((float(states.sensor.netatmo_kamer_temperature_2.state)) - (float(states.sensor.rotterdam_temperature.state))) | round(2) }}' 
        unit_of_measurement: 'Graden'
        friendly_name: 'Delta temp Kamer Binnen - buiten'
        icon_template: "mdi:thermometer"
2 Likes

Sorry for the multi-reply, just getting used to this forum interface (I’m used to the older forums types)

I know this isn’t exactly what you are looking for but it might give you some clues as to how to accomplish what you need.

This is actually super helpful, thanks! I couldn’t figure out how to do math in my template to give a deadband of 1 or 2 degrees.
We don’t have AC but it’s been in the 80s the past few days in Seattle area and the upstairs of the house gets really hot. I’ve been constantly checking the sensors manually in the evening to know when it’s time to open the windows and cool off the upstairs. My automation (hopefully) triggers one of the RGB indicators on my Homeseer WD200+ dimmer switch in the bedroom to be red when it’s hotter than outside and blue when it’s cooler than outside. Next step is to also add a push notification but I haven’t set those up yet so I’ll want to do some math to prevent getting spammed with notifications.

Glad I could help.

We have a room on the third floor that doesn’t stay as cool and it is the dog’s hangout spot when we are at work so I hooked up a window fan as an exhaust fan to help but I don’t want to have to mange it manually because it is a cheap fan that only has on/off, high/low. So I also wrote the below automation that turns it on at 8am on weekdays if the thermostat isn’t in “eco” mode (vacation mode) and if the weather forecast high calls for temps hotter than 73.

I’m thinking you may want to show a certain color light after 9pm on your WD200+ so you could open the windows at night to let cool air in if it is going to be hot the next day and then close them in the morning.

states.weather.yweather.attributes.forecast[0] is for today’s forecast and states.weather.yweather.attributes.forecast[1] woudl be for tomorrows.

# Dog Fan
- id: turn_on_dog_fan
  alias: Timer- Turn On Dog Fan
  initial_state: 'on'
  trigger:
    - platform: time
      at: '08:00:00'
  condition:
    condition: and
    conditions:
      # If the tempature forecast high is greater than or equal to 74 degrees
      - condition: template
        value_template: '{{ states.weather.yweather.attributes.forecast[0].temperature >= 74 }}'
      # If Nest is not in Away/Eco mode
      - condition: template
        value_template: '{{ states.climate.house_nest.attributes.operation_mode != "eco" }}'
      # If it is a weekday
      - condition: template
        value_template: '{{ now().weekday() in (0,1,2,3,4) }}'
  action:
    - service: switch.turn_on
      data:
        entity_id:
          - switch.dog_fan

And actually I’d recommend using the states() function, like this:

value_template: ‘{{states(‘sensor.outdoor_temp’)|float > states(‘sensor.bedroom_temperature’)|float}}’

This works (I fixed a parenthesis error) in the dev tool but not in my automations.yaml

I get:

Error loading /config/configuration.yaml: while parsing a block mapping in "/config/automations.yaml", line 169, column 5 expected <block end>, but found '<scalar>' in "/config/automations.yaml", line 170, column 32

The without the states() function works fine for now.

You were right about the string comparison. In the dev tool:

{{states.sensor.outdoor_temp.state < '173'}}
Give False

Oops, copy and paste error. Sorry about that. I went back and fixed my post for future reference.

That’s weird. It definitely should work. I guess I’d have to see exactly what was in your config when you got that error. Probably another copy and paste error. :wink: Oh well, at least you have it working!

Oddly enough my configuration checks valid when I take the single quotes off the entity_id like this and it seems to be working.

value_template: ‘{{states(sensor.outdoor_temp)|float > states(sensor.bedroom_temperature)|float}}'

But ONLY for the trigger. When I set up push notifications to my phone and created a message using a data_template, it wouldn’t work without the single quotes. This took me about 4 hours last night to discover…

I can post my entire automation code tonight if it helps. I’m pretty curious if I’m just not understanding the syntax or if this is a bug of some kind

I’d be happy to look it over if you can post it.

Thanks! I seriously appreciate it.

Here is the automation I’m trying to get working:

- alias: 'Bedroom hot notify'
  trigger:
    platform: template
    value_template: '{{states(sensor.bedroom_temperature)|float > (states(sensor.outdoor_temp)|float + 2) and states(sensor.bedroom_temperature)|float > 72|float}}'
  action:
    service: notify.rathouse
    data_template:
      title: "Bedroom Hot"
      message: "The bedroom is {{states('sensor.bedroom_temperature')}}°, {{states('sensor.bedroom_temperature')|float - states('sensor.outdoor_temp')|float}}° hotter than outside."

I was wrong and the value_template for the trigger isn’t working now. It says valid config but doesn’t work and apparently I’ve been logging “UndefinedError: ‘sensor’ is undefined” all day. When I try something like this (simplified function):

value_template: '{{states('sensor.outdoor_temp')|float > states('sensor.bedroom_temperature')|float}}'

I get:
Error loading /config/configuration.yaml: while parsing a block mapping in "/config/automations.yaml", line 169, column 5 expected <block end>, but found '<scalar>' in "/config/automations.yaml", line 170, column 32

Line 169 is one line above that - “platform: template”

Okay, I’m just a dummy.
I believe the problem was I need double quotes around the entire function. Here is my full automation which appears to be working now (changed the message a little too):

- alias: 'Bedroom hot notify'
  trigger:
    platform: template
    value_template: "{{states('sensor.bedroom_temperature')|float > (states('sensor.outdoor_temp')|float + 2|float) and states('sensor.bedroom_temperature')|float > 71|float}}"
  action:
    service: notify.rathouse
    data_template:
      title: "Bedroom Hot"
      message: "The bedroom is {{states('sensor.bedroom_temperature')|int}}°.  It is {{states('sensor.bedroom_temperature')|int - states('sensor.outdoor_temp')|int}}° cooler outside."

Thanks again. PS - push notifications are super cool!

I was wondering if maybe that was the case (i.e., single quotes both inside the string and enclosing the string.) That doesn’t work, of course. (Although, I suppose you could do '{{ states(\'sensor.bedroom_temperature\') ... }}', but that would be silly! :wink:)

BTW, sorry I didn’t respond earlier, but I’ve been a bit distracted.

No problem! I’m just glad to have some help.

So I thought this was done and put to rest but now I’m getting the push notification repeatedly. I think it is every time the value of the sensor changes (either outdoor_temp, bedroom_temperature, or both I’m not sure)
I assumed HA would only re-trigger the automation when the value_template goes false, then turns true again (tomorrow).
Is there a quick fix for this? I’m mostly seeing examples of people adding timers or delays but their problems are slightly different.
Could I create a binary sensor from the value template and monitor that sensor instead of the value template?

The template trigger is going to be re-evaluated (and the automation run if it evaluates to true, even if it was already true) anytime any of the entities referenced by it change. And that includes all changes, not just “the states.” I.e., an entity’s state is considered to have changed if its actual state changes, or even if any of its attributes change.

This is important with state triggers, too. If you don’t use to: or from: with a state trigger, then attribute changes can also trigger the automation.

So, yes, I think that’s a good idea. Create a binary_sensor using the same template as your current trigger, then change the trigger to a state trigger with the binary_sensor and to: 'on'. Then it will only trigger when the template goes from false to true.