Helper, Script? Generate a room darkness category

I am looking to generate a contant for each room in a synthetic sensor lux.roomname … with a value of "Pitch Black, Dark, Dim, Light, Bright’ for each room by evaulating the lux value from sensors I have in each of 12 rooms.’ Then I could check to see what is a room a Dim to set early evening or morning lighting, etc.

New to HA, how would you look to do this?

this can be done with a value template.

I don’t know your values. I just copy/pasted and modified I had from an existing wind_sensor.

light_sensor:
   value_template: >
     {% if (states.room_lux | int) >= 50 and (states.room_lux | int) <= 75 %}
       Bright
     {% elif (states.room_lux | int) >= 30 and (states.room_lux | int) <= 49 %}
       Useable
     {% elif (states.room_lux | int) >= 20 and (states.room_lux | int) <= 29 %}
       Dim
     {% elif (states.room_lux | int) >= 15 and (states.room_lux | int) <= 19 %}
       Dark
     {% elif (states.room_lux | int) >= 10 and (states.room_lux | int) <= 14 %}
       Darker
     {% elif (states.room_lux | int) >= 5 and (states.room_lux | int) <= 9 %}
       getting there
     {% else %}
       Pitch Black
     {% endif %}

Are the thresholds for each value the same for all 12 rooms?

For example, if “Dim” is between 80 and 50 in the bedroom, does “Dim” in the kitchen use the same threshold of 80 and 50? Or are the thresholds likely to be unique for each room?


EDIT

To help you get started, create a Template Sensor helper via:

  1. Settings → Device & Services → Helpers
  2. Create Helper → Template → Template a sensor

Give it a name and enter the following Jinja2 template into the State template field:

{% set lux = states('sensor.bedroom_lux') | int(0) %}
{{ { lux >= 100: 'Bright',
     60 <= lux < 100: 'Light',
     30 <= lux < 60: 'Dim',
     15 <= lux < 30: 'Dark',
     lux < 15: 'Pitch Black' }.get(true, 'unknown') }}
  • Replace sensor.bedroom_lux with the entity_id of your room’s actual light sensor.
  • Adjust the threshold values in the template to suit your preferences for that particular room.
  • In the Preview section, it will show you the result computed by the template.
  • If all is to your liking, click Submit.

1 Like

I’m very earlier in my templating, but starting to pick it up. I have a couple questions if you don’t mind.

What exactly does int(0) do? Also, what would int(1) do? Is it converting to an integer and rounding to (x) decimals?

What does .get(true, 'unknown') do?

The 0 in int(0) represents the default value (you choose what you want the value to be). If the supplied value cannot be converted to an integer, 0 will be the result.

If you overlook to supply int() with a default value, if will produce an error message if it is unable to convert the supplied value to an integer (i.e. the template fails and terminates further processing).

The template defines a dictionary (key-value pairs). The template is designed to produce keys that are boolean values (true/false). If the value of lux is within a specific range, that key will be true. The template is designed so that only one key can be true.

get is one of the methods of a dictionary. In this specific example, it attempts to get the value of whichever key is true. If none of the keys is true, get reports its default value (I chose unknown as the default).

2 Likes

Thank you! That was very helpful, and a great explanation.

1 Like

I am just getting started and will contribute when i am up to speed. I wanted to say thank you - this is an awesome community.

1 Like

I wanted to say thank you - this is an awesome community.

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

1 Like