Automation and service template + scripts

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 %}

And here’s the automation part:

- id: vesijohtoautomatiikka_temp
  alias: Vesijohto lämmitysautomatiikka
  description: 'Lämmitys ulkolämpötilan mukaan '
  trigger:
    - entity_id: sensor.testi_vesijohto
      platform: state
  action:
    - service_template: >
        {% if is_state('input_select.mode' , 'min') %}
          script.Vesijohto_min_skripti
        {% elif is_state('input_select.mode' , 'med') %}
          script.Vesijohto_med_skripti
        {% elif is_state('input_select.mode' , 'max') %}
          script.Vesijohto_max_skripti
        {% else %}
          script.Vesijohto_off_skripti
        {% 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.

Any help? I’m already chewing my nails hear…

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 %}

Thanks for simplifying. However, the original problem still exists. Any idea what’s wrong with the automation part?

Your service should be script.turn_on and what you have under service_template should be a data_template.

No, that’s not it. You can call a script directly in the service (or service_template.)

Have you tried manually triggering the automation to see if it runs the desired script?

Have you tried manually changing the state of sensor.testi_vesijohto to see if that triggers the automation (causing the desired script to run)?

Have you checked that the automation is turned on?

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.

Automation is turned on.

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.)

Entity names are always entirely lowercase.

The names of all your scripts use an uppercase V. Change them so they use a lowercase v.

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:

  action:
    - service_template: >
        {% if is_state('input_select.mode' , 'min') %}
          script.1588703034943

No luck.

Script itself looks like:

'1588703034943':
  alias: vesijohto_min_skripti
  sequence:
  - data: {}
    entity_id: automation.vesijohto_automaattinen_lammitys_min
    service: automation.turn_on
  - data: {}
    entity_id: automation.vesijohto_automaattinen_lammitys_med
    service: automation.turn_off
  - data: {}
    entity_id: automation.vesijohto_automaattinen_lammitys_max
    service: automation.turn_off

BTW, I’m sure some simplification could be made here as well, but it works…

How are you confirming the automation is triggered?

Are you observing messages in the log file and/or checking the automation’s last_triggered attribute?

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.

I just can’t get this to work.

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?

Uups. Glad you pointed that out. That’s clearly in wrong place.
Changed that and also renamed the scripts to be more descriptive.

- id: vesijohtoautomatiikka_temp
  alias: Vesijohto lämmitysautomatiikka
  description: 'Lämmitys ulkolämpötilan mukaan '
  trigger:
    - entity_id: sensor.testi_vesijohto
      platform: state
  action:
    - service_template: >
        {% if is_state('sensor.testi_vesijohto', 'min') %}
          script.min_skripti
        {% elif is_state('sensor.testi_vesijohto', 'med') %}
          script.med_skripti
        {% elif is_state('sensor.testi_vesijohto', 'max') %}
          script.max_skripti
        {% else %}
          script.off_skripti
        {% endif %}

The problem still exists. Only script that starts is off_skripti when state gets being polled again.

You can reduce the template to this:

    - service_template: "script.{{states('sensor.testi_vesijohto')}}_skripti"

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!

1 Like