Trying to Trigger Automation When One Netatmo Weather Station Reports Lower Temperature Than Another

I’m new to Home Assistant, and I’m trying to set up an automation that is based on the temperature readings from 2 different Netatmo Weather Station modules.

Specifically, one weather station module is inside, and the other is outside. I want to create an automation that will turn on a switch that is connected to a window fan whenever the outside temperature is equal to or less than the inside temperature. I have already set up the Netatmo integration, and the “Overview” page of HA shows all of the Netatmo sensors along with their temperature readings. The relevant entity ids are:

sensor.netatmo_bedroom_temperature ## (inside temperature)
sensor.netatmo_patio_temperature ## (outside termperature)

Based on other examples I found online, I inputted the following into my automations.yaml file:

automation:
- alias: 'Bedroom hotter than outside'
trigger:
platform: template
value_template: {{states.sensor.netatmo_bedroom_temperature.state >= states.sensor.netatmo_patio_temperature.state}}
action:
service: switch.turn_on
data_template:
entity_id: switch.window_fan

But I am getting the following error when I try to validate the configuration:

Error loading /config/configuration.yaml: invalid key: “OrderedDict([(‘states.sensor.netatmo_bedroom_temperature.state >= states.sensor.netatmo_patio_temperature.state’, None)])”
in “/config/automations.yaml”, line 6, column 0

Can anyone tell me what I am doing wrong?

single line templates require the templates to be wrapped in quotation marks. either single or double makes no difference. EXCEPT if you use quotation marks inside the template then the ones inside the template have to be opposite the ones outside.

and be sure to use the standard text style marks not the “fancy curly” ones that some word processors will use. as long as you are using a simple text editor then you should be fine.

Also, you don’t need to use “data_template:” in the service call because you aren’t using any templates there. just use “data:”. Or better yet since you are only passing it an entity_id the you don’t even need to use that.

Here is how it should look based on above:

automation:
  - alias: 'Bedroom hotter than outside'
    trigger:
      platform: template
      value_template: "{{states.sensor.netatmo_bedroom_temperature.state >= states.sensor.netatmo_patio_temperature.state}}"
    action:
      service: switch.turn_on
      entity_id: switch.window_fan

as an example of the different quotes requirement it would be something like this (and this is the recommended format so you might want to use this in place of you template anyway):

value_template: "{{states('sensor.netatmo_bedroom_temperature') >= states('sensor.netatmo_patio_temperature') }}"

And I’m not sure what you tried to format the code portion of the post but it wasn’t correct.

to format the code:

Awesome!

All is working now. Many thanks.

Will remember code formatting method in the future.

The values really need to be converted to numbers before doing the comparison. Otherwise you’re comparing strings (which all states are), and that won’t work as expected. E.g.:

So:

automation:
  - alias: 'Bedroom hotter than outside'
    trigger:
      platform: template
      value_template: >
        {{ states('sensor.netatmo_bedroom_temperature')|float >=
           states('sensor.netatmo_patio_temperature')|float }}
    action:
      service: switch.turn_on
      entity_id: switch.window_fan

you’re right, of course.

I honestly didn’t even look at the validity of the template. I was just focused on the formatting of the template to get rid of the error that was reported.

1 Like

Thanks. This explains a lot. I initially thought it was working, but soon observed some weird behavior, including fans turning off and on at the wrong times, or turning on only to immediately turn off again.

One question: your value_template code starts with a ‘>’ and end with a double quote ("), but there is no corresponding double quote. Is that a typo? should the ‘>’ at the beginning be the first double quote?

I am going to try the following:

value_template: “{{ states(‘sensor.netatmo_bedroom_temperature’)|float > states(‘sensor.netatmo_patio_temperature’)|float }}”

Yes. It’s a typo. I’ll fix it…