After a long time trying I’ve made a nice template sensor that calculates the current height of our local tide. It’s not elegant, but it works. My next challenge is to make its icon show whether the tide is rising or falling. I’m using two local variables from the tidal height calculation, but it looks like they are not available to the icon: statement. Can anyone help me resolve this?
template:
- sensor:
- name: height now
unique_id: height_now
state_class: measurement
unit_of_measurement: m
state: >
{% set last_tide = states.sensor.tide1.state %}
{% set last_ht = states.sensor.tide1h.state %}
{% set next_tide = states.sensor.tide1.state %}
{% set next_ht = states.sensor.tide1h.state %}
{%- if as_timestamp(states.sensor.tide1.state) >= as_timestamp(now()) %}
{% set next_tide = states.sensor.tide1.state %}
{% set next_ht = states.sensor.tide1h.state %}
{%- elif as_timestamp(states.sensor.tide2.state) >= as_timestamp(now()) %}
{% set last_tide = states.sensor.tide1.state %}
{% set last_ht = states.sensor.tide1h.state %}
{% set next_tide = states.sensor.tide2.state %}
{% set next_ht = states.sensor.tide2h.state %}
{%- elif as_timestamp(states.sensor.tide3.state) >= as_timestamp(now()) %}
{% set last_tide = states.sensor.tide2.state %}
{% set last_ht = states.sensor.tide2h.state %}
{% set next_tide = states.sensor.tide3.state %}
{% set next_ht = states.sensor.tide3h.state %}
{%- elif as_timestamp(states.sensor.tide4.state) >= as_timestamp(now()) %}
{% set last_tide = states.sensor.tide3.state %}
{% set last_ht = states.sensor.tide3h.state %}
{% set next_tide = states.sensor.tide4.state %}
{% set next_ht = states.sensor.tide4h.state %}
{%- elif as_timestamp(states.sensor.tide5.state) >= as_timestamp(now()) %}
{% set last_tide = states.sensor.tide4.state %}
{% set last_ht = states.sensor.tide4h.state %}
{% set next_tide = states.sensor.tide5.state %}
{% set next_ht = states.sensor.tide5h.state %}
{% endif -%}
{{last_ht | float + 0.5*(next_ht | float - last_ht | float) * (1-cos(3.1415926|float * (as_timestamp(now())-as_timestamp(last_tide))/(as_timestamp(next_tide)-as_timestamp(last_tide)))) }}
icon: >
{% if next_ht | float < last_ht | float %}
mdi:arrow-down
{% else %}
mdi:arrow-up
{% endif %}
Thanks Troon, that’s very clever & worked perfectly. I wish I knew enough to do that!
It makes me wonder whether I can simplify the multiscrape that generated all those tide times & heights. I’ve also got a list sensor.tide0 which contains 4 sets of 3 [state/time/height]:
Sorry, have I made a faux pas? Having accepted the limitation you pointed out, Troon has provided a more elegant & efficient block of code to repeat. FWIW I voted for your feature request, which I can see being useful in other areas I’m working on.
The purpose of the Solution tag is to answer/resolve the original question/problem. It helps lead other users to the post that helps them answer/resolve the original question/problem. For more information, see guideline 21 in the FAQ.
You asked:
Is there a work around that does not make me repeat the whole block of code
The answer is no; you must repeat it.
That’s the answer other users, seeking to solve the same problem, ought to be directed to. There’s no workaround; you have to repeat the code block.
Troon’s template is an elegant reduction of your original template but it isn’t a workaround for duplication. Whether you copy-paste 30 lines of template or just 8, you still have to make a complete duplicate.