Trying to change the 'class' and measurement value of a sensor

Hi, I am not very educated on what to do with YAML but I am trying to create a ‘sensor’ by adding the following to configuration.yaml

template:
    - sensor:
      - name: "Solar Panel Voltage"
        unit_of_measurement: "V"
        device_class: voltage
        state_class: measurement
#        state: "{{ states('sensor.Alpha ZTV123') | float(1.234) }}"
        state: "{{ states('sensor.Alpha ZTV123') / 10.0 }}"
#        state: "{{ 9.876 }}"

The sensor in question - ‘sensor.Alpha ZTV123’) is something I hacked together on an ESP32 from an example temperature sensor and so it presents as a Temperature Device measuring on degrees C - my modified version measures a DC voltage. I have added the device to a History Graph Card and it presents as expected:

(Purely for interests sake - the sensor is monitoring the output voltage of a solar panel and the dip on the top graph at around 7pm was last night’s solar eclipse)

You can see that I have tried 3 different “states” lines (two commented out) and I have created a history card to show this sensor.
The first line always presents 1.234 which suggests that the attempt to change to a float is failing
The third line always presents 9.876 which suggests that the rest of the YAML is acceptable.

But using the middle line presents “unavailable” on the History Graph

I’m a bit lost as to what I need to do to make this work, any advice would be gratefully received.
PhilipJ

Try to use Settings/Tools#template , when “laborating” with templates
There you will get “another” idea what’s going on than the message you get in Card/UI

Another thing which is unclear to me , is it a temperature-sensor, or is it a volt sensor ? ( I mean you can’t take a temp-sensor and “convert it” to a volt-sensor
The “Dip” will ofcause occur either ways during Night and a Solar-eclipse

state: "{{ 'sensor.Alpha ZTV123') }} "

Should give you the state

You almost got it except entity ids do not contain spaces or capital letters. Check your entity id in settings → tools → states

If you use ESPHome to do this you can specify the correct unit and device class inside ESPHome. No extra template sensor required.

lol, i totally missed that … i just tried with one of my own sensors

Yep, this looks like a template sensor issue more than the voltage itself.

states('sensor.Alpha ZTV123') returns a string, so you need to convert it to a number first. Try:

template:
  - sensor:
      - name: "Solar Panel Voltage"
        unit_of_measurement: "V"
        device_class: voltage
        state_class: measurement
        state: "{{ states('sensor.Alpha ZTV123') | float / 10 }}"

If the source sensor sometimes goes unavailable, that can also make the template show unavailable. I’d check the source entity state in Developer Tools first.

If it still acts up, post the exact HA version and how that ESP32 sensor is being created, because that bit can matter a lot.

Please read my post above. You have made the same mistake.

Hi, thank you for your comment. I wasn’t aware of the templates section in tools, I was doing stuff by using the File Editor and writing my sensor directly into configuration.yaml
You have opened my eyes to a whole new area that I can explore…
regards
PhilipJ

Thanks for the pointer - I thought that was the correct name because it seemed to work fine if I put the original sensor into a history card it worked ok. However when I looked in Settings->Tools->States it was displayed as Alpha_ZTV123 so I tried that and it seems to have fixed it, My “Solar Panel Voltage” sensor now appears in my History Graph Card.

Nope. As I said entity ids do not have space or capital letters. It would be sensor.alpha_ztv123.