Hello!
I am trying to trigger an automation by temperature sensor state change. The problem is that I cannot get the automation part to work.
Although I am not really sure if the sensor template works at all I’m able to test it by altering the sensor state in developer tools.
Overall the idea would be to run different scripts depending on the outside temperature. Those scripts then turn on/off certain automations in heating.
Here’s the sensor config:
sensor:
- platform: template
sensors:
testi_vesijohto:
friendly_name: "testi vesijohto"
value_template: >-
{% if states('sensor.ulko.state') | int < 1 and states('sensor.ulko.state') | int > -5 %}
min
{% elif states('sensor.ulko.state') | int < -5 and states('sensor.ulko.state') | int > -10 %}
med
{% elif states('sensor.ulko.state') | int < -10 %}
max
{% else %}
off
{% endif %}
I have tested to run the scripts and they all work allright.
No error messages with this code, but it just doesn’t seem to call the scripts?
I can verify if the script had run by visually checking the state of the heating automation in entity card. If I run the script “manually”, the entity changes its state on/off.
states('sensor.ulko.state') should be states('sensor.ulko'). But you can simplify further:
sensor:
- platform: template
sensors:
testi_vesijohto:
friendly_name: "testi vesijohto"
value_template: >
{% set value = states('sensor.ulko') | int %}
{% if -5 < value < 1 %}
min
{% elif -10 < value < -5 %}
med
{% elif value < -10 %}
max
{% else %}
off
{% endif %}
Triggering the automation manually doesn’t run the script even if I had changed the state of sensor.testi_vesijohto manually to any of the values: min, med, max.
So changing the state of sensor.testi_vesijohto doesn’t trigger the automation, or at least the script doesn’t run.
If you manually trigger the automation (i.e., go to the Developer Tools page, STATES tab, then click on the “More Info” icon to the left of the automation, then click on the EXECUTE button) it should run one of the scripts. Have you checked the Logbook, or home-assistant.log, to see if the automation and/or script ran? Are you sure those are the entity IDs of the scripts? (Typically entity IDs are all lower case.)
OK, I changed the script names to lower case, but still no luck.
I did some more testing and the action part in automation that calls the scripts is definitely the culprit here.
Automation triggers when I change sensor status and running any of the scripts manually works (they start/stop some more automations…).So let’s focus on the action part next.
In developer states the scripts have entitys like script.1588703034943.
Tried to change it in automation:
I am not sure how it should appear in log (?), but instead I can verify it by checking the last_triggered attribute. It’s updating every time I manually change the sensor state.
Just to make sure: if I manually change the sensor state, say from off to min, I can see in developer tools that automation triggers (as it should). However, when looking at scripts from the script editor, I can see that only the “off scrip” gets triggered and not “min script”. This is of course the real situation now, since the temperature doesn’t go below zero outside at the moment. I think that “off script” gets triggered when sensor.ulko updates, which happens every 60 second.
I even tried setting the “min value” range in config so that “min script” should definitely run, but still only “off script” runs.
So maybe the automation’s service template is somehow faulty and doesn’t select correct value?
Could someone comment the automation part, and the service template part of it especially.
In your first post, the automation triggers on the state of sensor.testi_vesijohto which can be min, med, max, or off. However, the automation’s action does nothing with the sensor’s state. It uses the state of input_select.mode (which can be min, med, max). I would think the automation should use the same entity in both the trigger and action. Can you explain why they are different?
Hooray! That did the trick.
I still don’t know what was wrong in previous elif template?
Anyway, I want to thank you both Taras and Phil for better code and finally finding a working solution.
Super happy for you help guys!