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.63k stars 30.78k forks source link

Template not executing correctly #126713

Closed EinSchwerd closed 1 month ago

EinSchwerd commented 1 month ago

The problem

I created a template sensor to convert the UV value (number) to the UV level (string) as it displays better on the dashboard. I used the following template {% set u = (states.sensor.uv.state | int) %} {% set d = { 0: 'None', 1: 'Low', 2: 'Low', 3: 'Medium', 4: 'Medium', 5: 'Medium', 6: 'High', 7: 'High', 8: 'Very High', 9: 'Very High', 10: 'Very High', 11: 'Extreme', 12: 'Extreme' } %} {{ d[u] if u in d else 'Unknown' }}

When u is set to 0, d[u] returns null and the template resolves to Unknown. However if I change the value of key 0 in the dictionary to anything but 'None', the template resolves correctly.

What version of Home Assistant Core has the issue?

System Information version | core-2024.9.3 -- | -- installation_type | Home Assistant Supervised dev | false hassio | true docker | true user | root virtualenv | false python_version | 3.12.4 os_name | Linux os_version | 6.1.0-23-amd64 arch | x86_64 timezone | Australia/Sydney config_dir | /config
Home Assistant Community Store GitHub API | ok -- | -- GitHub Content | ok GitHub Web | ok HACS Data | ok GitHub API Calls Remaining | 5000 Installed Version | 2.0.1 Stage | running Available Repositories | 1420 Downloaded Repositories | 55
AccuWeather can_reach_server | ok -- | -- remaining_requests | 35
Home Assistant Cloud logged_in | true -- | -- subscription_expiration | 1 January 2018 at 11:00 relayer_connected | false relayer_region | null remote_enabled | false remote_connected | false alexa_enabled | true google_enabled | true remote_server | null certificate_status | null instance_id | 331e10cc91a84510ab0c9683e365c8ce can_reach_cert_server | ok can_reach_cloud_auth | ok can_reach_cloud | ok
Home Assistant Supervisor host_os | Debian GNU/Linux 12 (bookworm) -- | -- update_channel | stable supervisor_version | supervisor-2024.09.1 agent_version | 1.6.0 docker_version | 27.1.2 disk_total | 226.7 GB disk_used | 35.3 GB healthy | true supported | true host_connectivity | true supervisor_connectivity | true ntp_synchronized | true virtualization | supervisor_api | ok version_api | ok installed_addons | File editor (5.8.0), Mosquitto broker (6.4.1), Terminal & SSH (9.14.0), Samba share (12.3.2), Zigbee2MQTT (1.40.1-1), ESPHome (2024.9.1), Samba Backup (5.2.0), Studio Code Server (5.15.0), AdGuard Home (5.1.2), InfluxDB (5.0.0), ecowitt2mqtt (2024.06.2), TheengsGateway (1.11.4), Meross Local Broker Addon (0.0.1-alpha49), TasmoAdmin (0.30.5), rsync (1.7.0)
Dashboards dashboards | 2 -- | -- resources | 31 views | 15 mode | storage
Recorder oldest_recorder_run | 15 September 2024 at 20:12 -- | -- current_recorder_run | 25 September 2024 at 04:43 estimated_db_size | 1195.20 MiB database_engine | sqlite database_version | 3.45.3

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

  1. Create a floating point sensor named uv.

  2. Create a template sensor with the following template {% set u = (states.sensor.uv.state | int) %} {% set d = { 0: 'None', 1: 'Low', 2: 'Low', 3: 'Medium', 4: 'Medium', 5: 'Medium', 6: 'High', 7: 'High', 8: 'Very High', 9: 'Very High', 10: 'Very High', 11: 'Extreme', 12: 'Extreme' } %} {{ d[u] if u in d else 'Unknown' }}

  3. Set uv value to 3

  4. Output of template sensor should be 'Medium'

  5. Set uv value to 0

  6. Output of the template sensor should be 'None'

Actual: Output template sensor value is 'Unknown'

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

mib1185 commented 1 month ago

None is a keyword and means "there is nothing" therefor it is interpreted tu null which also means "there is nothing"

What is your wanted result in case of u = 0 ?

EinSchwerd commented 1 month ago

I wanted to display "None".

mib1185 commented 1 month ago

you can wrap the None into additional quotation marks like '"None"' - this will tell the jinja template engine to handle this as a string and to not interpret it

EinSchwerd commented 1 month ago

If I use additional quotation marks it will be displayed as the value, I will not have None as the value but "None". Additionally, I discovered that adding another dereferencing of d[0] while the value of u is 0 to the end of the template will return None None.

mib1185 commented 1 month ago

Because the template works as intend, this is not an issue, rather I think you're in need of support on how to write your template to achieve your wanted goals? I suggest you to try our Community Forum or join our Discord chat server to get the needes support Thanks! 👍