Cannot set value via template

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 %}

Am I using correct templating format?

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.

1 Like

Actually, sensor.nettosolartoday entity doesn’t exist yet. Wasn’t sure what I was doing to be honest :thinking: 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

So how would I go about creating that entity?

Thanks in advance for the help!

Were you trying to create a Template Sensor?

Perhaps something like this?

template:
  - sensor:
      - name: "Net Solar Today"
        unit_of_measurement: "Wh"
        device_class: energy
        state: >
          {% if today_at('23:00') <= now() <= today_at('01:00') + timedelta(days=1) %}
            0
          {% else %}
            {{ states('sensor.solis_energy_today') }}
          {% endif %}

Yeah indeed I was trying to create a template.
So I have put below in my configuration.yaml:

 - platform: template
    sensors:
      nettosolartoday:
        friendly_name: "Net Solar Today"
        unit_of_measurement: "kWh"
        device_class: energy
        state: >
          {% if today_at('23:00') <= now() <= today_at('01:00') + timedelta(days=1) %}
            0
          {% else %}
            {{ states('sensor.solis_energy_today') }}
          {% endif %}

No yaml errors but the entity nettosolartoday is not available.

Another entity (eg: nettogridtoday) in same file is available:

        nettogridtoday:
        friendly_name: Netto Grid Today
        unit_of_measurement: "kWh"
        value_template: "{{ states('sensor.total_consumption_daily')|float - states('sensor.total_injection_daily')|float}}"

Any clue as what I’m still doing wrong?

Thanks for the great feedback already!

Yes, you changed the example I suggested.

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.

Reference
Template Sensor

Okay, that’s my bad…
I was not aware that there was a new way and a legacy way.
Changing now to the new way.

Thanks a bunch for your patience!
Let me know if I can buy you a coffee or something :wink:

It would be appreciated but it’s entirely at your discretion.

Actually,
I did notice an issue :frowning:

Code used in configuration.yaml

template:
  - sensor:
      - name: "Net Solar Today"
        unit_of_measurement: "kWh"
        device_class: energy
        state: >
          {% if today_at('23:50') <= now() <= today_at('01:00') + timedelta(days=1) %}
            0
          {% else %}
            {{ states('sensor.solis_energy_today') }}
          {% endif %}

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?

Fixed the final issue by splitting up the statements:

- name: "Net Solar Today v3"
        unit_of_measurement: "kWh"
        device_class: energy
        state: >
          {% if today_at('23:50') <= now() %}
            0
          {% elif now() <= today_at('01:30') %}  
            0
          {% else %}
            {{ states('sensor.solis_energy_today') }}
          {% endif %}