Automation attribute trigger not working

I have an entity which contains the electricity prices for the next day in an attribute “data”. When the prices aren’t available yet, the “data” attribute is null according to the template tester. When the prices are available, the “data” attribute contains a list with the time ranges and prices.
I want an automation to send a Matrix message as soon as the prices for the next day are in. The Matrix part works just fine, however, I can’t get the automation to trigger. Isn’t it possible to trigger on an attribute change?

Code:

alias: Nordpool price notice
description: ""
triggers:
  - trigger: state
    entity_id: sensor.nordpool_prices_tomorrow
    attribute: data
    not_to:
      - null
      - unknown
      - unavailable
conditions: []
actions:
  - action: notify.matrix
    metadata: {}
    data:
      message: >-
        {% set minprice = state_attr('sensor.nordpool_prices_tomorrow','data') |
        map(attribute='price_per_kwh') | list | min %}

        {% set maxprice = state_attr('sensor.nordpool_prices_tomorrow','data') |
        map(attribute='price_per_kwh') | list | max %}

        Electricity prices tomorrow:

        Min. {{ state_attr('sensor.nordpool_prices_tomorrow','data') |
        map(attribute='price_per_kwh') | list | min | round(4) }} EUR/kWh from
        {{ as_timestamp((state_attr('sensor.nordpool_prices_tomorrow',
        'data')|selectattr('price_per_kwh','eq',minprice)|first)['start_time'])
        | timestamp_custom('%H:%M')}} to {{
        as_timestamp((state_attr('sensor.nordpool_prices_tomorrow',
        'data')|selectattr('price_per_kwh','eq',minprice)|first)['end_time']) |
        timestamp_custom('%H:%M')}}

        Max. {{ state_attr('sensor.nordpool_prices_tomorrow','data') |
        map(attribute='price_per_kwh') | list | max | round(4) }} EUR/kWh from
        {{ as_timestamp((state_attr('sensor.nordpool_prices_tomorrow',
        'data')|selectattr('price_per_kwh','eq',maxprice)|first)['start_time'])
        | timestamp_custom('%H:%M')}} to {{
        as_timestamp((state_attr('sensor.nordpool_prices_tomorrow',
        'data')|selectattr('price_per_kwh','eq',maxprice)|first)['end_time']) |
        timestamp_custom('%H:%M')}}
mode: single

Do you have an attribute called “Tomorrow valid”?

Nop, I populate the data myself through a trigger template:

- trigger:
  - platform: time_pattern
    minutes: "/10"
  - platform: homeassistant
    event: start
  action:
      - action: nordpool.get_prices_for_date
        data:
          config_entry: 01K9M0Q7P4CXP3BF2W44M2BKMH
          date: "{{ now().date() + timedelta(days=1) }}"
        response_variable: tomorrow_price
  sensor:
    - name: Nordpool Prices tomorrow
      unique_id: nordpool_prices_tomorrow
      state: "{{ now().isoformat() }}"
      attributes:
        data: >
          {% set all = namespace(prices=[]) %}
          {% if tomorrow_price and tomorrow_price['FI'] is defined %}
            {% for item in tomorrow_price['FI'] %}
              {% set all.prices = all.prices + [{
                'start_time': (item.start | as_datetime | as_local).isoformat(),
                'end_time': (item.end | as_datetime | as_local).isoformat(),
                'price_per_kwh': item.price / 1000 *1.255
              }] %}
            {% endfor %}
          {% endif %}
          {{ all.prices | sort(attribute='start_time') }}

Couldn’t a template trigger work using what you already have in your template?

{% if tomorrow_price and tomorrow_price['FI'] is defined %}

So I assume you would need

{{ states.sensor.nordpool_prices_tomorrow.attributes.data is defined }}

remove the not_to and add a condition

- condition: template
  value_template: "{{ trigger.to_state is not none and trigger.to_state.attributes.data is not none }}"
1 Like

I’ll try, thanks

Yeah, that worked, thanks again!
Why did my original config not work? Did I misunderstand the docs?

Because attributes can be any object type, and null is not something you can check against.

You were basically trying to treat an attribute like the main state, and they are completely different things.

1 Like