I have a very basic question about setting values via template.
Struggling with this and getting a bit crazy…
I want to create a new sensor value (nettosolartoday) that is set to 0 during 23h00 and 01h00 (when there is for sure no solar energy). This because I notice sometimes during the shift at night, there’s a small or large peak injected which makes the ‘max’ value NOK.
I have below “code” but in the templating part I don’t get it too work.
{% set state = states('sensor.nettosolartoday') %}
{% if '23:00' <= state <= '01:00' %}
0
{% elif '01:01' <= state <= '22:59' %}
sensor.solis_energy_today
{% endif %}
You can’t compare times like that in a template. You are actually comparing strings. Strings are compared left to right so for example ‘100’ would be less than ‘2’.
You are also returning the entity id and not the state value.
You can have as many elif statements as you want but you must have one else statement. In your example this is easy as it is all other times.
Probably the easiest way would be to do this:
{% set state = today_at(states('sensor.nettosolartoday')) %}
{% if today_at('23:00') <= state <= today_at('01:00') %}
0
{% else %}
{{ states('sensor.solis_energy_today') }}
{% endif %}
today_at() converts your time string to a datetime object with todays date and the specified time. Datetime objects will be compared as you expect.
Assuming you sensor.nettosolartoday entity actually contains an hh:mm time string.
Actually, sensor.nettosolartoday entity doesn’t exist yet. Wasn’t sure what I was doing to be honest Just trying out stuff…
Basically I wanted to create a new entity to correct that the sensor was giving strange readings between 23h00 and 1h00
There are two ways to define a Template Sensor, the modern way, that’s what’s used in the example I posted,
and the legacy way which is what you unsuccessfully tried to do with my example.
I checked this morning the value and noticed that between 00:00 and 01:00 the value of net_solar_today was not set to 0.
From 23:50 to 00:00 the value was set to 0, so that part is working…
Is there an error in the timedelta() function?