How do I import a HA template value as a number?

at the moment I am doing

text_sensor:
  - platform: homeassistant
    id: sonos_volume
    entity_id: sensor.media_volume
    internal: true


lambda: |- 
  if id(sonos_volume) > 10 {
    WHATEVER}

Error

src/main.cpp: In lambda function:
src/main.cpp:127:10: error: expected ‘(’ before ‘sonos_volume’
if sonos_volume > “10”

what am I doing wrong?

From a quick look at the lambda docs this appears to be the format you want:

lambda: |- 
  if (id(sonos_volume) > 10) {
    WHATEVER}

you’ll have problems using text_sensor and comparing it with a number, can you use just sensor?
If not you’ll have to convert a string to number in the lambda, that is c code, e.g. stoi

thanks for the code tom_I I will look into that.

yes I will look into using the sensor function instead.

do the two changes gets me

src/main.cpp: In lambda function:
src/main.cpp:135:26: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if (sonos_volume > 10) {
^
Compiling .pioenvs/sonos_display/libd05/ESP8266WiFi/WiFiUdp.cpp.o
Compiling .pioenvs/sonos_display/lib3f7/ESP8266mDNS/ESP8266mDNS.cpp.o
*** [.pioenvs/sonos_display/src/main.cpp.o] Error 1

sounds too complex for a happy hacker to complete maybe I am doing something that is easier done another way

what I want to achieve is

if HomeAssitant template value = 50
  it.printf("50");
Else
  it.printf("not 50");

Any bright ideas? Is there any easier way in HA to turn the template value into a number?

here is the template value from config

value_template: "{{ (state_attr('media_player.tv_speakers', 'volume_level') * 100 ) | int }}"

Don’t be afraid. We’ll get there.

id(sonos_volume) is returning a pointer and 10 is an integer so that’s why it is complaining.
Try accessing the state property.
To do so try id(sonos_volume).state if that does not work try id(sonos_volume)->state

report back!

1 Like

On all my media entities volume is a float between 0.0 and 0.99. maybe Sonos is different, but I don’t know why it would be.

1 Like

what is the say way to double check this?

Look in the developer states ui.

To do so try id(sonos_volume).state if that does not work try id(sonos_volume)->state

Either of those get me this

src/main.cpp:136:33: warning: multi-character character constant [-Wmultichar]

if (sonos_volume->state > '30') {

Got these but no idea how I can tell if they are floats

top one was a value template I created so I could import the status into ESPhome. Unless there is an easier way to get the Sonos Volume into ESPhome as a float.

Ohh the number 30 (or whatever literal number) should not have quotes of any kind.
'30' is a multi-character character constant, you don’t want that.

1 Like

You sir are a legend!!!

For the first time this complied, I am now off to test on the device in question.

I’m trying to use a sensor value from home-assistant in a esphome sensor but I get an error:
Expected integer, but cannot parse id(value_x).state as an integer.

The value in home-assistant is an integer…how can I pass it to esphome an still be integer ?

Thanks.