Use current sensor value in automation

Hi

Is this the correct way to display/use the current value of a sensor in an automation?

“{{ states.sensor.mysensorname.state }}”

Im struggling about my automation and not sure if has something to do with this change
Thanks

I think we’ll need to see where in your automation you are using it. If you are using a template to get the value of a sensor then you would generally use:

"{{ states('sensor.mysensorname') }}"

Yes I want to use it in an automation action:

service: notify.justme
data:
title: Market price!
message: >-
Market price increase Trigger {{
trigger.to_state.name }}.
summary: “{{ states.sensor.mysensorname.state }}”

Your automation action looks okay to me. You’ll need to try it. However, the syntax I gave in my previous message is generally better because it handkes sensors that are not ready in a better way.

Hm the error is
Error: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘to_state’

Please post the full automation.

The trigger object is only available when the automation is actually triggered, not when you “run” it through the menu. Furthermore, to_state is only available on some trigger types.

Also when you do, please format your pasted automation correctly for the forum, see: How to help us help you - or How to ask a good question

The automation:

alias: Market price increase
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.mysensor
    above: 5
    for:
      hours: 1
      minutes: 0
      seconds: 0
condition:
  - condition: time
    weekday:
      - fri
      - thu
      - wed
      - tue
      - mon
action:
  - service: notify.myself
    data:
      title: Market price 5 procent increase
      message: >-
        Market price 5 procent increase. Trigger {{
        trigger.to_state.name }}.
      summary: "{{ states('sensor.mysensor') }}"
mode: single

Doesn’t it work when I use a numeric_state as trigger then?

That should work, if you let the automation actually trigger (i.e. wait 1h with “mysensor” above 5)

Hmm the error stays

How are you testing this?

Manually but also with this trigger, the error is the same on both ways

  - platform: time_pattern
    minutes: /3

Works now. Don’t ask how. I think I was missing a quote somewhere
Thanks all!

Neither of those trigger methods have a “to_state”. Please familiarise yourself with the available variables for each trigger type, see: https://www.home-assistant.io/docs/automation/templating/#available-trigger-data and note that manually triggering has no trigger data, at all.

This is how:

1 Like