Create own sunshade sensor status


I have a sunshade and can control it through HomeAssistant with a IR relay box, so I have seperate buttons configured in HA to control the movement. This means HA does not know at what percentage the shade is rolled out, if this explanation makes sense :slight_smile: .

However, I thought of the following, but my program skills are zero so if somebody could help me code this that would be great!

What I would like to make is a template sensor that will display the latest action pressed of two of the three buttons.

So, the three buttons I have are

As you can see the status is a date and timestamp. What I would like to have is two (up and down) combined and show the STATE of the last pressed one, so it would be something like this.

{% if states("button.sunscreen.down" == PRESSED AS LAST %}
{% else states("button.sunscreen.up" == PRESSED AS LAST %}
{% endif %}

As I said, I’m an IT guy but I have no knowledge in programming, I can read it but I have no clue how to write or parse code for this kind of stuff. So if somebody can shine a light on this for me, that would be great! :slight_smile:

Something like this

{% if (as_timestamp(states("button.sunscreen.down"))) > (as_timestamp(states("button.sunscreen.up"))) %}
{% else %}
{% endif %}
1 Like

A slightly more complex way of doing it (over complicated) is to do it like this:

{% set buttons = { states('button.sunscreen.down'):state_attr('button.sunscreen.down', 'friendly_name'), states('button.sunscreen.up'):state_attr('button.sunscreen.up', 'friendly_name') } %}
{{ buttons[[states('button.sunscreen.down'), states('button.sunscreen.up')] | max] }} 

So this creates a list of [datetime] : friendly name of the buttons.
Then it takes the maximum value of the state as the key and finds the value (meaning the friendly name).

1 Like

Could’ve think of this myself lol. thanks!
It’s just looking what value is ‘earlier’ in this code, right? if Down is greater than up it will show Down otherwise up.

However, it does not ‘accept’ the current code.

To be honest, without ur explanation with it I couldnt read that piece of code lol.
When I try ur code, it returns NULL to me though.

It seems one or both of the states are unknown/null?

Edit… no there is a typo.
You have dot between sunscreen and up/down. It should probably be underscore

1 Like

Yes mate, working!!

Many thanks!

For completeness, Browets answer probably also works with the same fix.
We both just copied the entities you posted

If you want to remove “Zonnescherm” then you can do that with a replace:

{% set buttons = { states('button.sunscreen_down'):state_attr('button.sunscreen_down', 'friendly_name'), states('button.sunscreen_up'):state_attr('button.sunscreen_up', 'friendly_name') } %}
{{ buttons[[states('button.sunscreen_down'), states('button.sunscreen_up')] | max] | replace("Zonnescherm ", "") }} 

And it will only say UP or DOWN

1 Like

Oh lol I see, my bad!

Thanks for your replies mate!