stevemann
(Stephen Mann (YAML-challenged))
November 22, 2023, 11:42pm
1
I have a wireless thermometer that publishes the temperature in °C over MQTT. It works.
I tried to make a template sensor to create an entity in °F. It doesn’t work.
I suspect that I am not using the template sensor correctly, can someone help, please?
#In mqtt.yaml:
sensor:
- name: "Office Temperature"
unique_id: "office_temperature"
state_topic: "rtl_433/Zotac/devices/Oregon-v1/1/15/temperature_C"
unit_of_measurement: "°C"
#In sensors.yaml
- platform: template
sensors:
office_temperature_fahrenheit:
friendly_name: "Office Temperature (Fahrenheit)"
value_template: "{{ (states('sensor.office_temperature_celsius') | float * 9/5) + 32 }}"
unit_of_measurement: "°F"
nickh66
(Nick Harrison)
November 23, 2023, 12:45am
2
Hi,
For the sensor template you need to use “state” instead of “value_template”.
Nick
tom_l
November 23, 2023, 1:14am
3
Not if they are using legacy template sensors, which they are.
Steve, you should be using the new format for new sensors.
You can change the temperature unit from the frontend UI, but if you want another sensor with the different unit:
add a default value for your float filter. If you don’t this can prevent your sensor loading.
use the template editor to work out what is going on, e.g. what does this return:
{{ states('sensor.office_temperature_celsius') }}
{{ (states('sensor.office_temperature_celsius') | float(0) * 9/5) + 32 }}
How it would look in the new format:
# In configuration.yaml
template:
- sensor:
- name: "Office Temperature (Fahrenheit)"
state: "{{ (states('sensor.office_temperature_celsius') | float(0) * 9/5) + 32 }}"
availability: "{{ has_value('sensor.office_temperature_celsius') }}"
unit_of_measurement: "°F"
device_class: temperature
state_class: measurement
stevemann
(Stephen Mann (YAML-challenged))
November 23, 2023, 3:47am
4
Many thanks. I am trying to learn templates… There seems to be too many different types of templates for me to grok.
{{ states('sensor.office_temperature') }}
Retures: “18.9”
{{ (states('sensor.office_temperature') | float(0) * 9/5) + 32 }}
Returns: 66.02
With a few typo changes, your suggested template works:
template:
- sensor:
- name: "Office Temperature (Fahrenheit)"
state: "{{ (states('sensor.office_temperature') | float(0) * 9/5) + 32 }}"
availability: "{{ has_value('sensor.office_temperature') }}"
unit_of_measurement: "°F"
device_class: temperature
state_class: measurement
I can’t find availability
: in the docs. In another post you said: “Anything that can not be converted to an integer, like unavailable or unknown will be given the default value of -1.” How does has_value
change this? (Around here a value of -1 is a valid temperature).
.
That might be easier. How do I do this and where is it in the docs?
tom_l
November 23, 2023, 3:54am
5
Click on your sensor in the dashboard. In the pop-up window click on the settings cog icon. Units should be an option and will do the conversion for you if you change them.
stevemann
(Stephen Mann (YAML-challenged))
November 23, 2023, 5:27am
6
That’s what my notes said, but this is what the settings cog presents:
tom_l
November 23, 2023, 5:40am
7
Which integration is providing that temperature sensor?
What attributes does the sensor have?
It probably needs a device class of temperature.
stevemann
(Stephen Mann (YAML-challenged))
November 23, 2023, 8:20pm
9
Thanks for the link.
No integration. The values are coming over MQTT from an external broker. In my first post I define the sensor in mqtt.yaml
Just what I gave it when creating the sensor in mqtt.yaml.
tom_l
November 24, 2023, 2:55am
10
Give it a device class, restart, then see if you can change the unit via the UI:
#In mqtt.yaml:
sensor:
- name: "Office Temperature"
unique_id: "office_temperature"
state_topic: "rtl_433/Zotac/devices/Oregon-v1/1/15/temperature_C"
unit_of_measurement: "°C"
device_class: temperature
1 Like
stevemann
(Stephen Mann (YAML-challenged))
November 24, 2023, 3:52am
11
Thanks. I learned something today.