When templating a timestamp, what's a correct ELSE fallback?

Hi

I’m using this template:

value_template: >
          {% set dateraw_match = value | regex_findall('1\\-0\\:1\\.6\\.0\\(([0-9]{12})') %}
          {% if dateraw_match %}
            {{ as_timestamp(strptime(dateraw_match[0], '%y%m%d%H%M%S')) | timestamp_local }}
          {% else %}
            {{ "unavailable" }}
          {% endif %}

to read this data:

1-0:1.6.0(241204154500W)(05.473*kW)

from a Youless sensor RAW data page (W peak usage timestamp) with the REST integration.

When templating other data, like the actual W usage or W peak, I fallback to “{% else %} 0 …”
But what is the correct fallback for a timestamp? {{“unavailable”}} generates errors in the log.

“sensor.piekvermogen_tijd rendered invalid timestamp: unavailable”

I guess it depends on what you want to achieve. Unavailable is a valid state as obviously your sensor does not deliver a valid input.

In templates you for example could just reuse the last state, but I guess this won’t do in value_template and would still make no sense in your case with the youless:

value_template:
        ...
        {% else %}       
          {{ this.state }}
        {% endif %}

I don’t have such device. Can you show some examples the REST interface returns for the people to see the different cases?

Do I understand correctly that this timestamp can be of any time in the past when the device had it’s highest W peak?

Without further insight: Would it be possible to just save the tag as json_attributes? Then you can create a triggered template sensor which only updates when the highest W peak (timestamp) value is valid and has changed.

1 Like

The proper way is to add an “availability_template”:

value_template: >
  {% set dateraw_match = value | regex_findall('1\\-0\\:1\\.6\\.0\\(([0-9]{12})') %}
  {{ as_timestamp(strptime(dateraw_match[0], '%y%m%d%H%M%S', 0)) | timestamp_local }}
availability: >
  {{ value | regex_findall('1\\-0\\:1\\.6\\.0\\(([0-9]{12})') | count > 0  }}
2 Likes

Hi David. Tnx for looking into this.

My RAW Youless data looks like this:

/FLU5\253770234_A

0-0:96.1.4(50217)
0-0:96.1.1(3153414731313035303536353537)
0-0:1.0.0(241230121947W)
1-0:1.8.1(006398.440*kWh)
1-0:1.8.2(008522.350*kWh)
1-0:2.8.1(006626.781*kWh)
1-0:2.8.2(002535.571*kWh)
0-0:96.14.0(0001)
1-0:1.4.0(00.937*kW)
1-0:1.6.0(241204154500W)(05.473*kW)
0-0:98.1.0(13)(1-0:1.6.0)(1-0:1.6.0)(231201000000W)(231125190000W)(04.493*kW)(240101000000W)(231212163000W)(04.864*kW)(240201000000W)(240129183000W)(04.901*kW)(240301000000W)(240226183000W)(04.723*kW)(240401000000S)(240314190000W)(04.824*kW)(240501000000S)(240420200000S)(05.386*kW)(240601000000S)(240504194500S)(03.831*kW)(240701000000S)(240608194500S)(03.625*kW)(240801000000S)(240725193000S)(04.527*kW)(240901000000S)(240805193000S)(05.057*kW)(241001000000S)(240922193000S)(05.144*kW)(241101000000W)(241027183000W)(04.695*kW)(241201000000W)(241107180000W)(05.061*kW)
1-0:1.7.0(03.997*kW)
1-0:2.7.0(00.000*kW)
1-0:21.7.0(03.997*kW)
1-0:22.7.0(00.000*kW)
1-0:32.7.0(231.0*V)
1-0:31.7.0(017.89*A)
0-0:96.3.10(1)
0-0:17.0.0

So I’m using the REST sensor to retrieve data from that local URL hxxp://192.168.1.34/V?p=1

You are correct that 1-0:1.6.0 shows the watt peak usage date/time (in the past for the current month) and the actual peak usage in W.

I guess that sometimes the page returns blank due to some network error and in that case I would like to turn the sensor unavailable or unknown, but that doesn’t seem to be possible for a timestamp as the log shows “sensor.piekvermogen_tijd rendered invalid timestamp: unavailable”.
I’ve tried {{ None }}, {{ Unavailable }}, {{ Null }}, but the error keeps returning.

PLEASE understand that this is just a stupid error log and I can ignore it, but it’s my OCD kicking in and I’m trying to handle it correctly :sweat_smile:
For other templates (eg. the actual W peak usage) I can just use “0” ase “else” without showing errors.

Did not know availability_template is available with the REST sensor (platform).

1 Like

Aha! I’ll try this right away, tnx to both of you!
Ref: RESTful Sensor - Home Assistant

You mean the availability variable?

From my knowledge in case this returns false the sensor will be unavailable and that is the case you already have, don’t you?

Maybe my coffee helps. So you would still have a unavailable sensor but would get rid of your logging error as the timestamp will not be evaluated?

1 Like

Ah, missed it was REST. It’s plain availability:, then

1 Like

Yes, but returning “Unavailable” caused errors in the log as it was not a valid timestamp value. I believe with the availability set, it correctly enters the unavailable state and won’t attempt to render the value.

As some people would say in germany: “Again what learned” :grinning:

1 Like

If you make the else value {{ none }} (null) the state of the sensor will be unknown instead of unavailable, and the log will not complain about it. This is probably more accurate and more desirable that having a state of unavailable and you don’t need the availability parameter.

{% set dateraw_match = value | regex_findall('1\\-0\\:1\\.6\\.0\\(([0-9]{12})') %}
{% if dateraw_match %}
  {{ as_timestamp(strptime(dateraw_match[0], '%y%m%d%H%M%S')) | timestamp_local }}
{% else %}
  {{ none }}
{% endif %}
1 Like

Thanks @jazzyisj!

After some searching and reading … list all sensors with state unavailable none or unknown as well as @w35l3y comment in Difference between unknown available none… (my googling was bad as I did not find a source of truth in the HA docs :person_shrugging:) this seems to be the reasoning what to choose:

Rule #1: all states are strings. So you must not think of none being 0. It’s just the defined state whatever happened behind the scene.

  • unavailable in case of an error or as @w35l3y says if it does not have deterministic state. E.g. you don’t know if a binary sensor is true or false (on or off) although everything seemes fine with the setup and sensor.
  • unknown in case the sensor was not reached or setup yet. E.g. after booting homeassistant when the state was not set yet it’s often unknown
  • none could mean unknown or some other (hopefully) specified state of a sensor

So all the availability and template availability checks are to determine the deterministic state while just setting the state to unknown you flag it as “Right now I don’t know pal”

Would you agree?

1 Like