Automation doesn't trigger (solved)

@all how read this,
the Post 14 is the solution to my request, there are more solution if you read all post.

I’m a bit confused with my automation…

I would like to trigger an automation when this sensor goes from true to false, the whole thing is currently just a test automation that sends me a message via WhatsApp to check whether it triggers.

I don’t currently see why it shouldn’t trigger.

This is the history of the sensor

entity_id,state,last_changed
sensor.azimuth_strasse,True,2024-07-07T08:36:30.276Z
sensor.azimuth_strasse,False,2024-07-07T18:52:03.281Z
sensor.azimuth_strasse,True,2024-07-08T03:17:09.673Z
sensor.azimuth_strasse,False,2024-07-08T09:36:18.945Z
sensor.azimuth_strasse,True,2024-07-08T09:36:30.276Z

No matter what setting I use, there is no trigger

alias: Test Azimuth
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.azimuth_strasse
    from: null
    to: null
  - platform: state
    entity_id:
      - sensor.azimuth_strasse
    from: "true"
    to: "false"
  - platform: state
    entity_id:
      - sensor.azimuth_strasse
    from: "false"
    to: "true"
  - platform: state
    entity_id:
      - sensor.azimuth_strasse
    to: "False"
    from: "True"
  - platform: state
    entity_id:
      - sensor.azimuth_strasse
    to: "True"
    from: "False"
condition: []
action:
  - service: whatsapp.send_message
    data:
      clientId: default
      to: [email protected]
      body:
        text: Azimuth
mode: single

Solved since I use binary sensor.

First of all, stop guessing. Have a look at Developer Tools / States for the sensor and see what its real state is. Post a screenshot here.

Alternatively:

alias: Test Azimuth
description: ""
trigger:
  - platform: state
    entity_id: sensor.azimuth_strasse
condition: []
action:
  - service: whatsapp.send_message
    data:
      clientId: default
      to: [email protected]
      body:
        text: Azimuth changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}.
mode: single

:wink: I will not guess… I’m just surprised
this is current state of the sensor

I just don’t want to understand why the trigger doesn’t fire.

If I set the automation according to your, the trigger fire.

Because a trigger isn’t a condition. :slight_smile:

If your state already is true, it won’t trigger until this state changes. You need something else to trigger it, or you react in your trigger on other things. :slight_smile:

do you mean in the way of time pattern and the sensor as a condition?

According to your list, the sensor only changes two times a day. That’s where your trigger would fire. A trigger only fires, if the treshold is passed, not if it’s already in that state.

Eg. if you have a trigger for a thermometer above 20°C, it will only trigger, when the treshhold is passed, aka when the trigger goes from 20,0°C to 20,1°C. If the trigger is above 20°C already, it won’t trigger. :slight_smile:

I understand that, but it doesn’t trigger anyway.
According my first post, not even after two days.
Not even at the time when it changes from true to false

I usually use triggers that exceed a threshold as follows

{{ states('sensor.motion_east_illumination')|float >= 14000 }}

in this case I want to react explezier if change from true to false.

I don’t want to intrude Troons help here, so I take a step back and let him answer! :slight_smile:

Just one thing, please download the trace of the automation and post it here. It should show us, what’s going on. :slight_smile: You know where to find that?

What WhatsApp message did you get from my automation code?

Can I have a screenshot from the States (Zustände) tab please? The template editor can mislead you by trying to “help”:

Try this:

alias: Test Azimuth
description: ""
trigger:
  - platform: state
    entity_id: sensor.azimuth_strasse
condition:
  - "{{ trigger.to_state.state|lower == 'false' and trigger.from_state.state|lower == 'true' }}"
action:
  - service: whatsapp.send_message
    data:
      clientId: default
      to: [email protected]
      body:
        text: Azimuth changed from true to false.
mode: single
1 Like

the previous trace is completely empty because it was not triggered

These are the sensors I want to react to, each of these when changing from true to false, as I said the WhatsApp message is currently only for testing, behind each sensor is a custom template macro “If interested I can post this too”

OK, so it is True and False in uppercase — this is important. My trigger and condition above should work for you anyway by converting all to lowercase before comparing.

Where are these sensors defined? They feel like they should be binary sensors.

1 Like

these are the templates of the sensors shown

# tamplate

# Sonnenstand anhand der Himmelsrichtung und Tag im Jahr
  - sensors:
      azimuth_hof:
        friendly_name: 'Azimuth Hof'
        unique_id: 'azimuth_hof'
        value_template: >-
          {% from 'directional_azi.jinja' import directional_azi %}
          {{ directional_azi('hof', states('sensor.sonne_solar_azimuth')) }}
      azimuth_hofstrasse:
        friendly_name: 'Azimuth Hof Starsse'
        unique_id: 'azimuth_hofstrasse'
        value_template: >-
          {% from 'directional_azi.jinja' import directional_azi %}
          {{ directional_azi('hofstrasse', states('sensor.sonne_solar_azimuth')) }}
      azimuth_strasse:
        friendly_name: 'Azimuth Starsse'
        unique_id: 'azimuth_strasse'
        value_template: >-
          {% from 'directional_azi.jinja' import directional_azi %}
          {{ directional_azi('strasse', states('sensor.sonne_solar_azimuth')) }}
      azimuth_scheune:
        friendly_name: 'Azimuth Scheune'
        unique_id: 'azimuth_scheune'
        value_template: >-
          {% from 'directional_azi.jinja' import directional_azi %}
          {{ directional_azi('scheune', states('sensor.sonne_solar_azimuth')) }}

this is the corresponding macro
located in → custom_templates/directional_azi.jinja

{% set y_day_azi = {
1:
  {"hof": none,
  "hofstrasse": none,
  "strasse": [126,207],
  "scheune": [202,232]},
13:
  {"hof": none,
  "hofstrasse": none,
  "strasse": [123,207],
  "scheune": [202,236]},
26:
  {"hof": none,
  "hofstrasse": none,
  "strasse": [118,207],
  "scheune": [202,241]},
39:
  {"hof": none,
  "hofstrasse": none,
  "strasse": [114,207],
  "scheune": [202,245]},
52:
  {"hof": none,
  "hofstrasse": [109,128],
  "strasse": [109,207],
  "scheune": [202,252]},
65:
  {"hof": none,
  "hofstrasse": [99,128],
  "strasse": [99,207],
  "scheune": [202,260]},
78:
  {"hof": [94,106],
  "hofstrasse": [91,128],
  "strasse": [91,207],
  "scheune": [202,269]},
91:
  {"hof": [86,106],
  "hofstrasse": [82,128],
  "strasse": [82,207],
  "scheune": [202,277]},
104:
  {"hof": [78,106],
  "hofstrasse": [74,128],
  "strasse": [74,207],
  "scheune": [202,285]},
117:
  {"hof": [70,106],
  "hofstrasse": [66,128],
  "strasse": [66,207],
  "scheune": [202,294]},
130:
  {"hof": [67,106],
  "hofstrasse": [63,128],
  "strasse": [63,207],
  "scheune": [202,297]},
143:
  {"hof": [61,106],
  "hofstrasse": [57,128],
  "strasse": [57,207],
  "scheune": [202,300]},
156:
  {"hof": [57,106],
  "hofstrasse": [53,128],
  "strasse": [53,207],
  "scheune": [202,306]},
169:
  {"hof": [55,106],
  "hofstrasse": [51,128],
  "strasse": [51,207],
  "scheune": [202,308]},
182:
  {"hof": [55,106],
  "hofstrasse": [51,128],
  "strasse": [51,207],
  "scheune": [202,308]},
195:
  {"hof": [59,106],
  "hofstrasse": [54,128],
  "strasse": [54,207],
  "scheune": [202,304]},
208:
  {"hof": [63,106],
  "hofstrasse": [59,128],
  "strasse": [59,207],
  "scheune": [202,299]},
221:
  {"hof": [70,106],
  "hofstrasse": [59,128],
  "strasse": [59,207],
  "scheune": [202,292]},
234:
  {"hof": [78,106],
  "hofstrasse": [74,128],
  "strasse": [74,207],
  "scheune": [202,286]},
247:
  {"hof": [85,106],
  "hofstrasse": [81,128],
  "strasse": [81,207],
  "scheune": [202,278]},
260:
  {"hof": [93,106],
  "hofstrasse": [89,128],
  "strasse": [89,207],
  "scheune": [202,270]},
273:
  {"hof": [102,106],
  "hofstrasse": [98,128],
  "strasse": [98,207],
  "scheune": [202,261]},
286:
  {"hof": none,
  "hofstrasse": [106,128],
  "strasse": [106,207],
  "scheune": [202,253]},
299:
  {"hof": none,
  "hofstrasse": [114,128],
  "strasse": [114,207],
  "scheune": [202,246]},
312:
  {"hof": none,
  "hofstrasse": [120,128],
  "strasse": [124,207],
  "scheune": [202,239]},
325:
  {"hof": none,
  "hofstrasse": [124,128],
  "strasse": [124,207],
  "scheune": [202,234]},
338:
  {"hof": none,
  "hofstrasse": none,
  "strasse": [127,207],
  "scheune": [202,232]},
351:
  {"hof": none,
  "hofstrasse": none,
  "strasse": [126,207],
  "scheune": [202,232]},
} %}
{% macro directional_azi(direction, current_azimuth) %}
{% set yd = now().timetuple().tm_yday %}
{% set keys = y_day_azi.keys() | list %}
{% set start_ind = ((keys + [yd])|sort).index(yd) - 1 %}
{% set dir_dict = y_day_azi.get(keys[start_ind]) %}
{% set dir_list = dir_dict.get(direction) %}
{{ false if dir_list is none else current_azimuth|int
in range(dir_list[0], dir_list[1]) }}
{% endmacro %}

If the azimuth is within the specifications in the table then automations are allowed to perform certain things, otherwise they are prohibited / blocked

Don’t get me wrong, of course I could ask how I do “A or B” but I still want to understand why some things are the way they are.

You have defined all of those azimuth sensors as legacy sensors (under sensor: in your config) rather than binary sensors (under binary_sensor:), which is why you’ve had difficulty with the states.

Put them under binary_sensor: instead and they will all have a state of 'on' or 'off'.

Alternatively, use the UI to create template binary sensor helpers and don’t worry about the YAML:

Many thanks, I didn’t know that, will have a try.

1 Like

Ok, I added binary sensors via the UI and created a test automation. Let’s see if it works in a few hours, but it looks plausible.

alias: "Test Azimuth binary_sensor "
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.azimuth_scheune_binary
    from: "on"
    to: "off"
condition: []
action:
  - service: whatsapp.send_message
    data:
      clientId: default
      to: [email protected]
      body:
        text: Azimuth Scheune changed from on to off.
mode: single
1 Like

Feedback…
After two days, it works as it shoul.
Many thanks.

1 Like

Just out of curiosity, what is it, that you’re doing with this? :slight_smile:

Sure…
I want to use it to control the shutters on the roof windows, as they have different orientations north, east, south, west, I want to keep the light incidence as large as possible but the heating up as low as possible. I have determined the azimuth for each side of the house at intervals of 13 days a year using the website https://www.suncalc.org
(Info: the year can best be divided into 13 day intervals)
and as soon as the sun leaves this azimuth the blinds are raised again.

Currently I am stuck on the templet of position position _template within the automation, supposedly within data: is an illegal character

sorry that I used some German words within the automation.

choose:
  - conditions:
      - condition: and
        conditions:
          - condition: trigger
            id:
              - 15 Minuten Raster
          - condition: state
            entity_id: binary_sensor.azimuth_strasse_binary
            state: 'on'
            for:
              hours: 0
              minutes: 0
              seconds: 30
    sequence:
      - service: cover.set_cover_position
        metadata: {}
        data:
          position_template: >-
            {% if
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            >= 0 and
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            < 4500 %} 100 {% elif
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            >= 4500 and
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            < 5400 %} 90 {% elif
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            >= 5400 and
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            < 6300 %} 80 {% elif
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            >= 6300 and
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            < 7200 %} 70 {% elif
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            >= 7200 and
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            < 8100 %} 60 {% elif
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            >= 8100 and
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            < 9000 %} 50 {% elif
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            >= 9000 and
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            < 9900 %} 40 {% elif
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            >= 9900 and
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            < 10800 %} 30 {% elif
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            >= 10800 and
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            < 11700 %} 20 {% elif
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            >= 11700 and
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            < 12600 %} 10 {% elif
            states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float
            >= 12600 %} 0 {% endif %}
        target:
          entity_id: cover.speicher_scheune
  - conditions:
      - condition: trigger
        id:
          - Azimuth Straße Aus
    sequence:
      - service: cover.open_cover
        metadata: {}
        data: {}
        target:
          entity_id: cover.speicher_scheune
  - conditions:
      - condition: and
        conditions:
          - condition: trigger
            id:
              - 15 Minuten Raster
          - condition: state
            entity_id: binary_sensor.azimuth_scheune_binary
            state: 'on'
            for:
              hours: 0
              minutes: 0
              seconds: 30
    sequence:
      - service: cover.set_cover_position
        metadata: {}
        data:
          position_template: >-
            {% if
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            >= 0 and
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            < 10000 %} 100 {% elif
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            >= 10000 and
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            < 12000 %} 90 {% elif
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            >= 12000 and
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            < 14000 %} 80 {% elif
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            >= 14000 and
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            < 16000 %} 70 {% elif
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            >= 16000 and
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            < 18000 %} 60 {% elif
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            >= 18000 and
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            < 20000 %} 50 {% elif
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            >= 20000 and
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            < 22000 %} 40 {% elif
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            >= 22000 and
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            < 24000 %} 30 {% elif
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            >= 24000 and
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            < 26000 %} 20 {% elif
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            >= 26000 and
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            < 28000 %} 10 {% elif
            states('sensor.bewegungsmelder_scheune_west_beleuchtungsstarke')|float
            >= 28000 %} 0 {% endif %}
        target:
          entity_id: cover.speicher_strasse
  - conditions:
      - condition: trigger
        id:
          - Azimuth Scheune Aus
    sequence:
      - service: cover.open_cover
        metadata: {}
        data: {}
        target:
          entity_id: cover.speicher_strasse
1 Like

It’s position not position_template:

You can reduce that template a bit:

{% set x = states('sensor.bewegungsmelder_strase_ost_beleuchtungsstarke')|float(0) %}
{{ ((0, 100 - (((x // 900) - 4) * 10), 100)|sort)[1] }}

Here’s what I used in the template editor to double-check that my logic matches yours:

{% for x in range(0, 14000, 50) -%}
x: {{ x }}, mine: {{ ((0, 100 - (((x // 900) - 4) * 10), 100)|sort)[1] }}, yours: {% if x >= 0 and x < 4500 %} 100 {% elif x >= 4500 and x < 5400 %} 90 {% elif x >= 5400 and x < 6300 %} 80 {% elif x >= 6300 and x < 7200 %} 70 {% elif x >= 7200 and x < 8100 %} 60 {% elif x >= 8100 and x < 9000 %} 50 {% elif x >= 9000 and x < 9900 %} 40 {% elif x >= 900 and x < 10800 %} 30 {% elif x >= 10800 and x < 11700 %} 20 {% elif x >= 11700 and x < 12600 %} 10 {% elif x >= 12600 %} 0 {% endif %}
{% endfor %}

With a step of 450 rather than 50: