Create own sunshade sensor status

Hi,

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 %}
Down
{% else states("button.sunscreen.up" == PRESSED AS LAST %}
Up
{% 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"))) %}
   Down
{% else %}
   Up
{% 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!