home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.51k stars 30.71k forks source link

template engine, break over a sensor starting with a number #82654

Closed dennis-bell closed 1 year ago

dennis-bell commented 1 year ago

The problem

for my 3d printer I have a temperature sensor installed, called: sensor.3dprinter_temperature

Wanted to use this for an automation, and so I used the following template as condition:

  trigger:
  - platform: state
    entity_id: sensor.3dprinter_temperature
  condition:
    - condition: or
      conditions:
      - condition: template
        value_template: '{{ ((states.sensor.3dprinter_temperature.state | float) - (states.sensor.3dprinter_stats.attributes.min_value | float) | abs) >= (10) }}'
      - condition: template
        value_template: '{{ ((states.sensor.3dprinter_temperature.state | float) - (states.sensor.3dprinter_stats.attributes.max_value | float) | abs) >= (10) }}'
  action:
    - service: notify.dennis
      data_template:
        message: 'There is a significant temperature change of 10+ degrees in the last hour on {{states.sensor.attic_temperature.name}}'

Which throws the following exception in the logging: TemplateSyntaxError: expected token ')', got 'dprinter_temperature'

Been able to reduce it to:

{{ ((states.sensor.3dprinter_temperature.state | float) - (states.sensor.3dprinter_stats.attributes.max_value | float) | abs) >= (10) }}

Still throwing the same error. Until I changed the name of the sensor into this: sensor.printer3d_temperature Now it completely works ?!

What version of Home Assistant Core has the issue?

2022.11.2

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Supervised

Integration causing the issue

No response

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

tdejneka commented 1 year ago

image

Reference: Some more things to keep in mind

It's recommended to use the states() function to get an entity's value instead of direct referencing.

{{ states('sensor.3dprinter_temperature') | float(0) }}

image

dennis-bell commented 1 year ago

Thanks @tdejneka Ignore my issue, and I appreciate the feedback!