How to replace numerical sensor data with strings?

Hello community,

today I installed Home Assistant (version 0.96.5). With a NodeMCU microcontroller I added the TSL2165 lux sensor to Home Assistant and the sensor appear to deliver values correctly. So far so good.

Is it somehow possible to display the sensor data as strings instead of numerical values? For example, for a lux range from 0 to 50 display the value “dark” and for a lux range higher than 50 display the value “light”.

Any help is appreciated.

Best
Wong

Create a template sensor that sets string values based on the values of the lux sensor.

1 Like

So like this?

sensor:

  • platform: tsl2561
    name: “TSL2561 Ambient Light”
    address: 0x39
    update_interval: 60s
    value_template: >-
    {% if is_state(‘sensor.lux’, ‘>50’) %)
    Light is on
    {% else %}
    Light is off
    {% endif %}

No… like this:

Well this is the exact resource I based my previous post on.
I used the “renaming sensor output” example and tried to apply it to my case. What did I do wrong?

Well, your platform says “tsl2561” when the platform should be “template” as shown on that page, for starters.

A template sensor is a entirely new sensor. It doesn’t replace your existing sensor. It uses your existing sensor to create a new sensor that works the way you want it to.

Like this:

sensor:
  - platform: template
    sensors:
      my_new_sensor:
        friendly_name: "My New Sensor"
        value_template: >-
          {% if states('sensor.lux') | float > 50 %)
            light
          {% else %}
            dark
          {% endif %}

However, if you really only want two states (light and dark) and you’re not set on having the strings be there, you just want a way to know if a sensor is over a certain amount, then a binary_sensor template is better.

Like this:

binary_sensor:
  - platform: template
    sensors:
      is_it_light:
        friendly_name: "Is It Light"
        device_class: light
        value_template: >-
          {{ states('sensor.lux') | float > 50 }}

This sensor would be “on” when it’s over 50 and “off” otherwise.

1 Like

gotta change those quotes in the templates, those are the fancy quotes and they’ll cause a configuration to fail

1 Like

Ha. That’s what I get for copy/pasting the OPs code and editing it.

1 Like

Thanks for the in-depth answer. I will try it out later. You are absolutely right, i didnt use the platform “template”.

This helped me understand a lot in particular: “A template sensor is a entirely new sensor. It doesn’t replace your existing sensor. It uses your existing sensor to create a new sensor that works the way you want it to.”

Thank you very much

1 Like

I just tried your solution and get the follwing error:
[sensors] is an invalid option for [binary_sensor.template]. Please check the indentation.

This is my .yaml file:

sensor:
  - platform: dht
    pin: D7
    model: dht22
    temperature:
      name: "MS_temperature"
    humidity:
      name: "MS_humidity"
    update_interval: 5s
    
  - platform: tsl2561
    name: "MS_light"
    address: 0x39
    update_interval: 5s
    gain: 16x
  
binary_sensor:
  - platform: gpio
    pin: D4
    name: "MS_motion"
    device_class: motion
    
  - platform: template
    sensors:
      light_status:
        friendly_name: "light status"
        device_class: light
        value_template: >-
          {{ states('sensor.ms_light') | float > 50 }}
    
output:
  - platform: gpio
    pin: D5
    id: gpio_d1
    
switch:
  - platform: gpio
    pin: D5
    name: "LED"
    
i2c:
  sda: D2
  scl: D1
  scan: True

What am i still doing wrong?

The lux sensor i am operating has the name “sensor.ms_light”

Your configuration appears correct.

This is the second time I’ve seen someone complain that this type of configuration was giving an error.

My configuration works, though, I do it differently than you.

In configuration.yaml

binary_sensor: !include binary_sensors.yaml

in binary_sensors.yaml

- platform: template
  sensors: !include_dir_merge_named binary_sensors_template/

in binary_sensors_template/any_file_name.yaml

light_status:
  friendly_name: "light status"
  device_class: light
  value_template: >-
    {{ states('sensor.ms_light') | float > 50 }}

Functionally they are the same. I just like to have mine broken up for easier editing.

As of right now I have only worked with ESPHome and it created the following .yaml:
/config/esphome/node_multisensor.yaml

I just downloaded the Configurator to have a look at my configuration.yaml. I do not see the “node_multisensor.yaml” in it. Could that be the problem? Do I have to create these templates in the configuration.yaml? If so, why do these other sensors work while not being in the configuration.yaml either?

I do have heard before that its smart to split up the .yaml files. But to be honest, right now I just need a proof of concept for myself. Making it more enjoyable to edit in the future is definately on the list.

ESPHome YAML is for the specific device. It doesn’t go in configuration.yaml. ESPHome uses it to configure the device itself.

The ESPHome device then connects to Home Assistant in a way that adds entities without needing any information in configuration.yaml.

You don’t need to split your configuration up. Especially if you’re just starting out. I only pointed out that mine was split to suggest why, if there is a bug in Home Assistant, you are seeing it and I am not.

Check your tabs vs spaces, as that can throw things off, but I don’t think it is in this case. Make sure that all of your indentation either uses tabs or spaces, but not both (spaces is generally considered better).

If that doesn’t solve anything, I would remove everything you pasted above from configuration.yaml EXCEPT for the binary_sensor.template. Then see if you get the same error. Then slowly add the pieces back in, one-by-one, until the error comes back.

1 Like

Thank you very much for taking the time to help a beginner. Really appreciate it, even though my problem this isnt solved.

I will let you (and everyone who might read this) know what was the error if i can find it. Weird thing is that even when I delete everything else from the .yaml and copy/paste the example code, the error still persists…

Cheers

And if you take out the binary_sensor.template code and put everything else back, it works?

Yeah, everything works. As soon as i paste the example code, the error occurs.

I will try later to put the example code in the configuration.yaml. Someone on reddit mentioned that templating might not be supported by esphome.
If that doesnt work, im out of ideas.

templating is NOT supported by ESPHome. And if that’s where you’re putting the binary_sensor.template code that’s exactly the problem.

If you want the ESPHome device to actually create this binary sensor, instead of having it created in Home Assistant, you’ll probably need to use a lambda in ESPHome, though, I’m not fluent enough in it to help with the configuration.

I guess pasting the code into the configuration.yaml doesnt do the trick, does it?
At least i can confirm that the code is being accepted in the configuration.yaml file. The new sensor however does not appear.

I guess i will be reading into this then: https://esphome.io/guides/automations.html

Thanks again !! Your support means more to me than you might think. Keeps me motivated!

It should be all you need, really. Just paste it in there, in addition to having your ESPHome device working. Check HomeAssistant’s error log to make sure nothing went wrong with the binary_sensor, and it should show up in dev-states.

To close this issue, I thought I’ll deliver a quick update.
This code indeed worked when the sensor “ms_light” exists in the configuration.yaml.

sensor 123:
  - platform: template
    sensors:
      my_new_sensor:
        friendly_name: "My New Sensor"
        value_template: >-
          {% if states('sensor.ms_light') | float > 20 %}
          There is light!
          {% endif %}

I guess back then everything was too new and overwhelming for me. Thanks again. Just used this thread again to get it to work - finally.

Cheers