jouster
(Jon Andrews)
September 4, 2022, 12:57pm
1
I have some tide info being pulled from a sensor and it gives a number of attributes, one of which being the time of the next high tide
using template editor, this
{{ state_attr(‘sensor.west_mersea_tide’, ‘next_high_tide_at’) }}
spits out this
2022-09-04 18:12:39+01:00
I’d like to strip out everything apart from the time (and ideally down to hours and minutes only I guess) which I think needs a split but Im totally lost if honest so thought I’d come to those in the know
Whilst I am at it, another attribute from the same sensor
{{ state_attr(‘sensor.west_mersea_tide’, ‘next_high_tide_height’) }}
gives this
4.5m
so I’d like to use this as a condition so if the number is greater than say 5.2, display the tide…is this a templating thing as well?
Assuming that your sensor’s output is a datetime object you can use:
{{ (state_attr('sensor.west_mersea_tide', 'next_high_tide_at' ) | as_local).strftime("%-I:%M")}}
If it’s a string you will need to convert it to a datetime object:
{{ (state_attr('sensor.west_mersea_tide', 'next_high_tide_at' ) | as_datetime | as_local).strftime("%-I:%M")}}
It can be done with a template, but you may be able to use a Numeric State trigger if the unit symbol “m” isn’t actually part of the output. Otherwise, you will need to use a template:
trigger:
- platform: template
value_template: >
{{ (state_attr('sensor.west_mersea_tide', 'next_high_tide_height' )
| replace("m","") | float(0)) > 5.2 }}
2 Likes
finity
September 4, 2022, 1:25pm
3
If the date isn’t a datetime object (it most likely is tho since it is an attribute) then you can use this to extract just the hour and minutes from the string:
{{ state_attr(‘sensor.west_mersea_tide’, ‘next_high_tide_at’).split(' ')[1][:-9] }}
1 Like
jouster
(Jon Andrews)
September 5, 2022, 4:22pm
5
trigger:
- platform: template
value_template: >
{{ (state_attr('sensor.west_mersea_tide', 'next_high_tide_height' )
| replace("m","") | float(0)) > 5.2 }}
This one worked perfectly
however, when trying others I get errors again
this one
{{ (state_attr(‘sensor.west_mersea_tide’, ‘next_high_tide_at’ )|as_datetime|as_local).strftime("%-I:%M")}}
gave
TypeError: float() argument must be a string or a real number, not ‘datetime.datetime’
jouster
(Jon Andrews)
September 5, 2022, 4:23pm
6
unfortunately, this one
{{ state_attr(‘sensor.west_mersea_tide’, ‘next_high_tide_at’).split(’ ')[1][:-9] }}
didn’t work either and returned
UndefinedError: ‘datetime.datetime object’ has no attribute ‘split’
finity
September 5, 2022, 4:45pm
7
That’s because you can’t split a datetime object. Only a string (and probably a number?) can be split like that.
You will need to use Didgeridrew’s format for that.
it’s already a datetime so you can’t convert it from a datetime to a date time.
try this:
{{ (state_attr('sensor.west_mersea_tide', 'next_high_tide_at' )|as_local).strftime("%-I:%M")}}
Hi Drew / everybody.
acoording to this post I thought it was also the solution to my problem but it isn’t (quite yet).
Could you pleasse help : The EPIC Time Conversion and Manipulation Thread! - #359 by Doctor_JaKe