Help with simple automation: type: turn_on

Hi, All.
Please help me with automation. All I want to do is to switch on the plug when a specific value (avarege price which I calculated somewhere else) drops below 1. Here’s the code which doesn’t work:

`id: ‘…’

alias: PlugON
description: Switches on the plug when the average price is below 1

trigger:

  • platform: template
    value_template: ‘{{ states(’‘sensor.nordpool_percent_to_average’’) < 1}}’
    condition:
  • condition: device
    type: is_off
    device_id: …
    entity_id: switch.plug
    domain: switch
    action:
  • service: notify.mobile_app_phone
    data:
    message: The price is low; activating now
  • type: turn_on
    device_id: …
    entity_id: switch.plug
    domain: switch
    mode: single`

I have no idea why this does not work. Please help.

Please format your code correctly:

Most likely reason is that your template is comparing the string state of your sensor (states are always strings) with a number. To fix that, use:

value_template: "{{ states('sensor.nordpool_percent_to_average')|float(0) < 1}}"

Better still, use a numeric state trigger which does that conversion for you:

trigger:
  - platform: numeric_state
    entity_id: sensor.nordpool_percent_to_average
    below: 1

Thank you!
Could you also explain the basic principle of triger which I still do not understand - does the trigger listens to the value constantly or the trigger triggers on change of the value? Sorry for this unclear question but I home you understand. I want to understand when HA checks if states(’‘sensor.nordpool_percent_to_average’’) < 1 and fires the action

If you have a template in a trigger, HA will evaluate that template whenever any of the entities in the template changes; and the trigger will fire if the template output changes from false to true.

In this case, HA will evaluate your template whenever sensor.nordpool_percent_to_average changes; but because the sensor state is a string, not a number, it will never evaluate to less than the number 1. You need to change it to a number with states("sensor.nordpool_percent_to_average")|float(0) as described.

Much more information in the docs:

Please learn how to format code. For blocks of code, surround it with three backticks:

```
block of code
```

and for inline code, surround with a single backtick `inline code`.

If you don’t do that, the forum software hides your indentation and messes up the quotes with “smart quotes” — see what it’s done to your previous posts.