How to use a 'state trigger' to update a template sensor with attributes (for deconz daylight phases)

In my templates.yaml (with a lot of functioning template sensors), I am trying to use a ‘state trigger’ to update the template sensor ‘Latest Deconz Times’ and its attributes everytime the state of the sensor sensor.deconz_daylight changes.

I have studied the documentation on:

and have come up with the following code:

- sensor:
    trigger:
      - platform: state
        entity_id:
          - sensor.deconz_daylight
    name: Latest Deconz Times
    icon: mdi:weather-sunny
    state: >-
      {{ as_timestamp(now()) }}
    attributes: >-
      {% if states('sensor.deconz_daylight')=='nadir' %}
        nadir: {{ as_timestamp(now()) }}
      {% elif states('sensor.deconz_daylight')=='night_end' %}
        night_end: {{ as_timestamp(now()) }}
      {% elif states('sensor.deconz_daylight')=='nautical_dawn' %}
        nautical_dawn: {{ as_timestamp(now()) }}
      {% elif states('sensor.deconz_daylight')=='dawn' %}
        dawn: {{ as_timestamp(now()) }}
      {% elif states('sensor.deconz_daylight')=='sunrise_start' %}
        sunrise_start: {{ as_timestamp(now()) }}
      {% elif states('sensor.deconz_daylight')=='sunrise_end' %}
        sunrise_end: {{ as_timestamp(now()) }}
      {% elif states('sensor.deconz_daylight')=='golden_hour_1' %}
        golden_hour_1: {{ as_timestamp(now()) }}
      {% elif states('sensor.deconz_daylight')=='solar_noon' %}
        solar_noon: {{ as_timestamp(now()) }}
      {% elif states('sensor.deconz_daylight')=='golden_hour_2' %}
        golden_hour_2: {{ as_timestamp(now()) }}
      {% elif states('sensor.deconz_daylight')=='sunset_start' %}
        sunset_start: {{ as_timestamp(now()) }}
      {% elif states('sensor.deconz_daylight')=='sunset_end' %}
        sunset_end: {{ as_timestamp(now()) }}
      {% elif states('sensor.deconz_daylight')=='dusk' %}
        dusk: {{ as_timestamp(now()) }}
      {% elif states('sensor.deconz_daylight')=='nautical_dusk' %}
        nautical_dusk: {{ as_timestamp(now()) }}
      {% elif states('sensor.deconz_daylight')=='night_start' %}
        night_start: {{ as_timestamp(now()) }}
      {% endif %}

This code throws me the following error:

Invalid config for 'template' at template_sensors.yaml, line 1111: 'trigger' is an invalid option for 'template', check: sensor->0->trigger Invalid config for 'template' at template_sensors.yaml, line 1111: expected a dictionary for dictionary value 'sensor->0->attributes', got None
Line 1111 is:  `- sensor`
Line 1112 is:  `    trigger`

The error surprises me as in the docs Trigger-based template sensors the used yaml should work, but the error message says trigger is an invalid option

What am I doing wrong?

- trigger:
    - ...
  action:
    - ...
  sensor:
    - name: ...
      ...
    - name: ...
      ...
1 Like

image

Thank you @Ildar_Gabdullin! Your post #2 has really put me on the right track.

Now this code works for me:

# DECONZ: LATEST TIMES OF CHANGES DECONZ SUN PHASES
    # WHEN EVENT 'sensor.deconz_daylight' changes state THEN update the attributes of 'sensor.deconz_daylight_latest_times' CONDITIONALLY:
    # - IF sensor.laatste_deconz_tijden.state == sensor.deconz_daylight_latest_times.[attribute_name] THEN :
    #     >>  Store the current time to the [attribute_name] that matches the changed new state of 'sensor.deconz_daylight'
    # - IF sensor.laatste_deconz_tijden.state != sensor.deconz_daylight_latest_times.[attribute_name] THEN:
    #     >>  Re-store the old value of the attribute from the stored value in its respective m_* attribute variable

- trigger:
    - platform: state
      entity_id:
        - sensor.deconz_daylight
  action: []
  sensor:
    name: Deconz Daylight Latest Times
    icon: mdi:weather-sunny
    state: >-
      {{ now().strftime('%d-%m-%Y om %H:%M:%S') }}
    attributes:
      datum: >-
        {{ now().strftime('%d-%m-%Y') }}
      nadir: >-
        {% set m_nadir=state_attr('sensor.deconz_daylight_latest_times','nadir') %}
        {% if states('sensor.deconz_daylight')=='nadir' %} {{ as_timestamp(now()) }} {% else %} {{ m_nadir }} {% endif %}
      night_end: >-
        {% set m_night_end=state_attr('sensor.deconz_daylight_latest_times','night_end') %}
        {% if states('sensor.deconz_daylight')=='night_end' %} {{ as_timestamp(now()) }}  {% else %} {{ m_night_end }} {% endif %}
      nautical_dawn: >-
        {% set m_nautical_dawn=state_attr('sensor.deconz_daylight_latest_times','nautical_dawn') %}
        {% if states('sensor.deconz_daylight')=='nautical_dawn' %} {{ as_timestamp(now()) }}  {% else %} {{ m_nautical_dawn }} {% endif %}
      dawn: >-
        {% set m_dawn=state_attr('sensor.deconz_daylight_latest_times','dawn') %}
        {% if states('sensor.deconz_daylight')=='dawn' %} {{ as_timestamp(now()) }}  {% else %} {{ m_dawn }} {% endif %}
      sunrise_start: >-
        {% set m_sunrise_start=state_attr('sensor.deconz_daylight_latest_times','sunrise_start') %}
        {% if states('sensor.deconz_daylight')=='sunrise_start' %} {{ as_timestamp(now()) }}  {% else %} {{ m_sunrise_start }} {% endif %}
      sunrise_end: >-
        {% set m_sunrise_end=state_attr('sensor.deconz_daylight_latest_times','sunrise_end') %}
        {% if states('sensor.deconz_daylight')=='sunrise_end' %} {{ as_timestamp(now()) }}  {% else %} {{ m_sunrise_end }} {% endif %}
      golden_hour_1: >-
        {% set m_golden_hour_1=state_attr('sensor.deconz_daylight_latest_times','golden_hour_1') %}
        {% if states('sensor.deconz_daylight')=='golden_hour_1' %} {{ as_timestamp(now()) }}  {% else %} {{ m_golden_hour_1 }} {% endif %}
      solar_noon: >-
        {% set m_solar_noon=state_attr('sensor.deconz_daylight_latest_times','solar_noon') %}
        {% if states('sensor.deconz_daylight')=='solar_noon' %} {{ as_timestamp(now()) }}  {% else %} {{ m_solar_noon }} {% endif %}
      golden_hour_2: >-
        {% set m_golden_hour_2=state_attr('sensor.deconz_daylight_latest_times','golden_hour_2') %}
        {% if states('sensor.deconz_daylight')=='golden_hour_2' %} {{ as_timestamp(now()) }}  {% else %} {{ m_golden_hour_2 }} {% endif %}
      sunset_start: >-
        {% set m_sunset_start=state_attr('sensor.deconz_daylight_latest_times','sunset_start') %}
        {% if states('sensor.deconz_daylight')=='sunset_start' %} {{ as_timestamp(now()) }}  {% else %} {{ m_sunset_start }} {% endif %}
      sunset_end: >-
        {% set m_sunset_end=state_attr('sensor.deconz_daylight_latest_times','sunset_end') %}
        {% if states('sensor.deconz_daylight')=='sunset_end' %} {{ as_timestamp(now()) }}  {% else %} {{ m_sunset_end }} {% endif %}
      dusk: >-
        {% set m_dusk=state_attr('sensor.deconz_daylight_latest_times','dusk') %}
        {% if states('sensor.deconz_daylight')=='dusk' %} {{ as_timestamp(now()) }}  {% else %} {{ m_dusk }} {% endif %}
      nautical_dusk: >-
        {% set m_nautical_dusk=state_attr('sensor.deconz_daylight_latest_times','nautical_dusk') %}
        {% if states('sensor.deconz_daylight')=='nautical_dusk' %} {{ as_timestamp(now()) }}  {% else %} {{ m_nautical_dusk }} {% endif %}
      night_start: >-
        {% set m_night_start=state_attr('sensor.deconz_daylight_latest_times','night_start') %}
        {% if states('sensor.deconz_daylight')=='night_start' %} {{ as_timestamp(now()) }}  {% else %} {{ m_night_start }} {% endif %}


If you are interested, you can simplify the templates like this:

- trigger:
    - platform: state
      entity_id:
        - sensor.deconz_daylight
  action:
    - variables:
        mode: '{{ trigger.to_state.state }}'
        ts: '{{ now().timestamp() }}'
        attr: '{{ this.attributes[mode] }}'
  sensor:
    name: Deconz Daylight Latest Times
    icon: mdi:weather-sunny
    state: "{{ now().strftime('%d-%m-%Y om %H:%M:%S') }}"
    attributes:
      datum: "{{ now().strftime('%d-%m-%Y') }}"
      nadir: "{{ iif(mode == 'nadir', ts, attr) }}"
      night_end: "{{ iif(mode == 'night_end', ts, attr) }}"
      nautical_dawn: "{{ iif(mode == 'nautical_dawn', ts, attr) }}"
      dawn: "{{ iif(mode == 'dawn', ts, attr) }}"
      sunrise_start: "{{ iif(mode == 'sunrise_start', ts, attr) }}"
      sunrise_end: "{{ iif(mode == 'sunrise_end', ts, attr) }}"
      golden_hour_1: "{{ iif(mode == 'golden_hour_1', ts, attr) }}"
      solar_noon: "{{ iif(mode == 'solar_noon', ts, attr) }}"
      golden_hour_2: "{{ iif(mode == 'golden_hour_2', ts, attr) }}"
      sunset_start: "{{ iif(mode == 'sunset_start', ts, attr) }}"
      sunset_end: "{{ iif(mode == 'sunset_end', ts, attr) }}"
      dusk: "{{ iif(mode == 'dusk', ts, attr) }}"
      nautical_dusk: "{{ iif(mode == 'nautical_dusk', ts, attr) }}"
      night_start: "{{ iif(mode == 'night_start', ts, attr) }}"
1 Like

Nice! I am very interested. I did not know about the immediate-if IIF() in Jinja2.
Very DRY code.

@123 But somehow your refactored code is not being triggered. So it does not work for me (yet) and I can’t figure out why it is not being triggered. It does not throw any errors.