Need Help with Syntax

I’ve read thru several threads on this topic, and I’m just not getting it.

Does anyone know of a good video guide that explains formatting? The documentation isn’t very helpful for me, because I am a visual and auditory learner primarily… The other way is hard and takes longer due to brain injury i got when i was 4 months.

I am trying to copy this configuration that I got from a video explaining scripts:

variables:
  weather_condition: |
    {% set temperature = state_attr("sensor.high_river_temperature","temperature") %}
    {% if (temperature < -20) %}
      {% set condition = "Cold" %}
    {% elif (temperature >=-20 and temperature < 5) %)
      {% set condition = "Chilly" %}
    {% elif (temperature >=5 and temperature < 15) %}
      {% set condition = "Cool" %}
    {% elif (temperature  >=15 and temperature < 25) %}
      {% set condition = "Warm" %}
    {% elif (temperature >=25) %}
      {% set condition = "Hot" %}
    {% endif %} 
sequence:
  - stop: End
    response_variable: weather_condition
mode: single
icon: mdi:weather-dust

When I tried to save it, I got this message:

Message malformed: invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['variables']['weather_condition']

so i deleted it and entered it line by line, and it stopped on this one:

{% elif (temperature >= -20 and temperature < 5) %)

any suggestions on where I am messing up?

It has to be a curly bracket.
If you like you can shorten the condition template by using a dictionary:


    {% set condition = {
     temperature < -20: "Cold",
     -20 <= temperature < 5: "Chilly",
     5 <= temperature < 15: "Cool",
     15 <= temperature < 25: "Warm",
     temperature >=25: "Hot"
    } %}
    {{ condition.get(true) }}

Blockquote It has to be a curly bracket

Blockquote

Doh! I can’t believe I missed that… Geesh… so I corrected that and it saved ok, and I tried it your way and it still saved

If I run the script I get no errors… but if I try to pass the value to my other script from an automation, I get another error:
“Call-service error. TypeError: ‘<’ not supported between instances of ‘NoneType’ and ‘int’”

Here was the original script I typed in from an example to pass a variable message to my phone

alias: "Notification: Send Custom Message to Phone"
description: sends the current weather condition to phone
trigger:
  - platform: state
    entity_id:
      - input_boolean.dummy
    to: "on"
condition: []
action:
  - service: script.weather_get_temperature
    data: {}
    response_variable: result
  - service: script.notify_send_message_to_phone
    data:
      Title: Weather Condition
      message: The Current condition is {{ result.value }}
mode: single

Are you sure sensor.high_river_temperature has an attribute named temperature?

Your response variable needs to return a dictionary. I’ve set the key of the dictionary to match what you used in the calling script for the notification.

variables:
  weather_condition: |
    {% set temperature = state_attr("sensor.high_river_temperature","temperature") %}
    {% set condition = {
    temperature < -20: "Cold",
    -20 <= temperature < 5: "Chilly",
    5 <= temperature < 15: "Cool",
    15 <= temperature < 25: "Warm",
    temperature >=25: "Hot" } %}
    {{ {"value": condition.get(true)} }}
sequence:
  - stop: End
    response_variable: weather_condition
mode: single
icon: mdi:weather-dust

I’m actually not sure of anything at this point. I was following the examples in this video https://www.youtube.com/watch?v=vD_xckjQxRk except I replaced the weather service they called with the environment Canada data for my local town, I’ll have to see if I can get it to return a value…

I was following this because it comes closest to what I actually wanted to do, which is check for the temperature… If its cold, and I’ve been away, and I’m 15 K from down, set the thermostat to 22C. If it’s hot, send the RF code to turn on my portable ac…

Go the States tool find your sensor then share what you see there.

If the temperature value is in the center column like:

You need to use the state, not the attribute.

Let us know what you see (or post a screen grab) and we can modify the template accordingly.

What were you planning on using as the trigger for this?

From what you have described I don’t really see a need to go through a response variable. If you trigger off your distance going below 15K, then you can just check the sensor value as a normal condition (or as the conditions in a Choose action if you want to have both “heat on” and “ac on” sequences in the same automation).

Welcome to the (large) club.
It might help to paste your yaml code into a YAML LINT.

If you make a single error the LINT utility will probably give you dozens of errors. Just fix the first one and run it through LINT again.

Blockquote
What were you planning on using as the trigger for this?

From what you have described I don’t really see a need to go through a response variable. If you trigger off your distance going below 15K, then you can just check the sensor value as a normal condition (or as the conditions in a Choose action if you want to have both “heat on” and “ac on” sequences in the same automation).

Blockquote

The trigger was two conditions 1. I’ve actually left my home tiown at some points 2. When returning home, I’m about 15 KM away from home…

I’m already 90% sure that i can accomplish this, what I was having trouble with was checking the state of the sensor… I tried using state based commands but I couldn’t get it to work… Then I saw the video I linked and it was the first thing I’ve found that I could even halfway understand what the heck was going on.

I have severe brain injury due to lack of oxygen, it doesn’t mean that I can’t do things, it just takes me a lot longer… Writing and Reading are the worst for me… It’s like going from the west coast all the way to the east coast, but you have to fly to china first… You still get there, you’re just exhausted, tired and cranky when you do… :slight_smile:

All i really want to do right at this moment is how to extract a value from a sensor

speaking of which, you were right the value is in the middle… the sensor in question is -2.8

Home Assistant automation triggers are event driven. You can have multiple possible triggers, but only one event can be the trigger for a given run of an automation. Often it is best practice for the trigger to be the event closest in time to when you want the actions executed.

Ok, so we just need to adjust the template.

variables:
  weather_condition: |
    {% set temperature = states("sensor.high_river_temperature")| int(0) %}
    {% set condition = {
    temperature < -20: "Cold",
    -20 <= temperature < 5: "Chilly",
    5 <= temperature < 15: "Cool",
    15 <= temperature < 25: "Warm",
    temperature >=25: "Hot" } %}
    {{ {"value": condition.get(true)} }}
sequence:
  - stop: End
    response_variable: weather_condition
mode: single
icon: mdi:weather-dust
1 Like

Thank you so much, I knew there was a way to convert a string into an integer…