Attribute as trigger isn't working

Hi All,

Try to use the attribute part in trigger. But then it won’t work. Or is this only work with a numeric_state ?
Without:

      from: 'false'
      to: 'true'
      attribute: stock_available

Everything work, but its not the best way.
All sensors have a attribute stock_available with value True or False (with a capital T or F)
When I change the value in developer tool nothing send out as notification.

Here the whole script:

- alias: "ToGoodToGo - All shops"
  trigger:
    - platform: state
      entity_id: 
        - sensor.toogoodtogo_broodhuys_meijer_magic_box
        - sensor.toogoodtogo_albert_heijn_amsterdamseweg_amstelveen_magic_box
        - sensor.toogoodtogo_albert_heijn_to_go_amsterdam_wtc_magic_box
        - sensor.toogoodtogo_boulangerie_noe_gustav_mahlerlaan_amsterdam
        - sensor.toogoodtogo_de_drie_graefjes_stadionplein_amsterdam
        - sensor.toogoodtogo_hema_gelderlandplein
        - sensor.toogoodtogo_lidl_amsterdamseweg_188_groente_fruit_box
        - sensor.toogoodtogo_lidl_bankrashof_groente_fruit_box
        - sensor.toogoodtogo_starbucks_gustav_mahler_amsterdam
        - sensor.toogoodtogo_sushi_time_wtc_amsterdam_magic_box
        - sensor.toogoodtogo_the_roastary_magic_box
        - sensor.toogoodtogo_poke_perfect_zuidas_magic_box
        - sensor.toogoodtogo_juicebrothers_gustav_mahlerlaan_magic_box
        - sensor.toogoodtogo_le_pain_quotidien_gelderlandplein_magic_box
        - sensor.toogoodtogo_stadsbakker_jongejans_gelderlandplein_magic_box
      from: 'false'
      to: 'true'
      attribute: stock_available

  variables:
    stock: "{{ states(trigger.to_state.entity_id) }}"
    stock_available: "{{ state_attr(trigger.to_state.entity_id, 'stock_available') }}"
    price: "{{ state_attr(trigger.to_state.entity_id, 'price') }}"
    name: "{{ state_attr(trigger.to_state.entity_id, 'friendly_name') }}"

  action:
    - service: notify.mobile_app_peter_iphone
      data:
        message: "TooGoodToGo"
        title: "Stock: {{ stock }}, Price: {{ price }}, Name: {{ name }} "

So why do you put lowercase true/false for the from: and to:?

Both didn’t work.

from: 'false'
to: 'true'

or

from: 'False'
to: 'True'

Maybe it doesn’t trigger when you change it yourself vut only when the sensor attribute actually changes.

When I removed the 3 lines:

      from: 'false'
      to: 'true'
      attribute: stock_available

The trigger is working.
I change it myself and then I get notification … so it must work I guess…

Does it work if you remove the quotes? Without quotes it’s no longer a string but a boolean value.

Is not working also.
Also tried it with 1 entity. Didn’t work also…

- alias: "ToGoodToGo - All shops"
  trigger:
    - platform: state
      entity_id: sensor.toogoodtogo_broodhuys_meijer_magic_box
      from: 'false'
      to: 'true'
      attribute: stock_available
- alias: "ToGoodToGo - All shops"
  trigger:
    - platform: state
      entity_id: sensor.toogoodtogo_broodhuys_meijer_magic_box
      from: false
      to: true
      attribute: stock_available

Just out of curiosity, how does the attribute’s value appear in Developer tools > States? As lowercase true or propercase True?

This is whole list of all attributes:

price: 4.99
stock_available: false
url: http://share.toogoodtogo.com/item/9159
unit_of_measurement: portions
friendly_name: Broodhuys Meijer
icon: mdi:food-off
templates: 
icon_color: 'if (state === "on") return "rgba(251,214,67,1)"; return "rgba(71,116,157,1)";'

I suspect the State Trigger, when combined with attribute, doesn’t know how to support an attribute value with a boolean type.

You could also ignore the from/to in the trigger and add it as conditiom instead.

Tried that as well. But there my knowledge is lost… What I tried I didn’t get it work.
Struggle with the code… If you can help me I can try that

This is what I tried:

condition: "{{ trigger.to_state.attributes.stock_available == 'true' }}"

In the template, remove the quotes from 'true'. If the attribute’s type is boolean, it should work.

Try this,

condition: "{{ trigger.to_state.state.attributes.stock_available == true }}"

If the boolean is recognized correctly it should work like this as well:

condition: "{{ trigger.to_state.state.attributes.stock_available }}"

Its so strange. its not working.

I have a second automation. without all the attribute criteria. So when I change a sensor I get notification.
Only from that automation I receive things.

Nothing from the other with criteria when sensors goes from false to true…
If I change with False/false True/true the other test one is working…

very strange

In the Template Editor, this should return False if the attribute’s type is not a string.

{{ state_attr('sensor.toogoodtogo_broodhuys_meijer_magic_box', 'stock_available') is string }}

If it’s not a string then it stands to reason it must be a boolean. Yet it failed a direct comparison to a boolean. :thinking:

{{ state_attr('sensor.toogoodtogo_broodhuys_meijer_magic_box', 'stock_available') is string }}

output:

false

But when I change the value to “testest” then I get:

true

Tried it also without is string:

{{ state_attr('sensor.toogoodtogo_broodhuys_meijer_magic_box', 'stock_available') }}

output:

false

But when I change the value to “testest” then I get:

false

How do you change the value? As I said before, it probably doesn’t trigger on this manual change.

using developer tool > states > states attribute
change it there.

i change everything back with

      from: 'false'
      to: 'true'
      attribute: stock_available

and gonna wait when system change the value… :slight_smile:
I always thought you can check things by edit the value yourself

Got 4 updates by system itself. Every time I change a option that was given above.
none of them work…
:grimacing:

yes, I did reload automation after every change… haha

EDIT: Wait a minute… there is one solution that works:

###########################################################################################
# NOTIFY ME WHEN BOXES ARE IN STOCK AT TOGOODTOGO
###########################################################################################
- alias: "ToGoodToGo - All shops"
  trigger:
    - platform: state
      entity_id:
        - sensor.toogoodtogo_broodhuys_meijer_magic_box
        - sensor.toogoodtogo_albert_heijn_amsterdamseweg_amstelveen_magic_box
        - sensor.toogoodtogo_albert_heijn_to_go_amsterdam_wtc_magic_box
        - sensor.toogoodtogo_boulangerie_noe_gustav_mahlerlaan_amsterdam
        - sensor.toogoodtogo_de_drie_graefjes_stadionplein_amsterdam
        - sensor.toogoodtogo_hema_gelderlandplein
        - sensor.toogoodtogo_lidl_amsterdamseweg_188_groente_fruit_box
        - sensor.toogoodtogo_lidl_bankrashof_groente_fruit_box
        - sensor.toogoodtogo_starbucks_gustav_mahler_amsterdam
        - sensor.toogoodtogo_sushi_time_wtc_amsterdam_magic_box
        - sensor.toogoodtogo_the_roastary_magic_box
        - sensor.toogoodtogo_poke_perfect_zuidas_magic_box
        - sensor.toogoodtogo_juicebrothers_gustav_mahlerlaan_magic_box
        - sensor.toogoodtogo_le_pain_quotidien_gelderlandplein_magic_box
        - sensor.toogoodtogo_stadsbakker_jongejans_gelderlandplein_magic_box

  condition: "{{ trigger.to_state.attributes.stock_available == true }}"

  variables:
    stock: "{{ states(trigger.to_state.entity_id) }}"
    stock_available: "{{ state_attr(trigger.to_state.entity_id, 'stock_available') }}"
    price: "{{ state_attr(trigger.to_state.entity_id, 'price') }}"
    name: "{{ state_attr(trigger.to_state.entity_id, 'friendly_name') }}"

  action:
    - service: notify.mobile_app_peter_iphone
      data:
        message: "TooGoodToGo"
        title: "Stock: {{ stock }}, Price: {{ price }}, Name: {{ name }} "

YES YES

1 Like