If then time math for text in entity cards

Good morning,
I’ve got a pretty decent mushroom card dashboard set up, but I’m struggling to do some text manipulation based upon time. I have some basic text setup for primary information on one card, based upon time of day, it’ll greet me "Good morning, Good evening, etc.

{% set time = now().hour %}
{% if (time >= 18) %}
Good Evening, {{user}}!
{% elif (time >= 12) %}
Good Afternoon, {{user}}!
{% elif (time >= 5) %}
Good Morning, {{user}}!
{% else %}
Hello, {{user}}!
{% endif %}

I’m trying to take this one step further…

I have entity cards setup that display when the next sunrise is, next sunset is.

type: custom:mushroom-entity-card
entity: sensor.nextsunrise
tap_action:
action: none
hold_action:
action: none
double_tap_action:
action: none
name: Sunrise today at
icon_type: none

What I want to be able to do is change the “today” text, because obviously that next sunrise could be tomorrow.

So, as I’m writing this, it’s 9 am EST. The next sunrise value is 7:20 am, so what I need to do is take the next sunrise value, subtract the current time, which will give me a negative number. So long as it’s a negative number, I know the math has to work out and that value is tomorrow.

If it was 3 am currently, and sunrise was at 7:20 am, 7:20 - 3:00 = 4, so positive values would be for today.

I should be able to apply this to sunrise, sunset, moonrise, moonset and get that today/tomorrow value.
So, how do I implement this

      name: >-
        {% set time = now().hour % + now().minute)
        {% if (sensor.nextsunrise - time < 0) %}
        Sunrise is tomorrow at
        {% else %}
        Sunrise is today at
        {% endif %}

Just not sure of the context of the math in the first line. Can anyone provide assistance?

easier might be to use the sensor that says that the sun is belowhorizon
{{ states(‘sun.sun’) }} which give you either below_horizon or above_horizon

Copy-paste the following template into the Template Editor and experiment with it.

{% set sunrise = state_attr('sun.sun', 'next_rising') | as_datetime | as_local %}
Sunrise is {{ 'today' if sunrise.date() == now().date() else 'tomorrow' }} at

I don’t know anything about the value reported by your sensor.nextsunrise entity so the example I posted references the next_rising attribute of sun.sun. If you wish, you can modify the template to use your sensor.

That sort of works. Well, it actually does, but it’s presenting a new problem.

I can use that exact string in a template card, but I can’t get the time into the secondary field (want it on second line for spacing purposes)

type: custom:mushroom-template-card
primary: >-
  {% set sunrise = state_attr('sun.sun', 'next_rising') | as_datetime | as_local
  %}
  Sunrise is {{ 'today' if sunrise.date() == now().date() else 'tomorrow' }} at
secondary: hh:mm (am/pm)
icon: mdi:home
entity: sensor.nextsunrise

The entity card would allow me to use the time from the entity, but I’m unable to use that template you provided.

type: custom:mushroom-entity-card
entity: sensor.nextsunrise
tap_action:
  action: none
hold_action:
  action: none
double_tap_action:
  action: none
name: Next sunrise at
icon_type: none

I’ve been trying to play around with what you provided in the template editor to just parse that time as a second line in a hh.mm format like an entity card would do on it’s own, but I’m not having any luck.

If the template works but not in the card you’re using then it’s due to some requirement/limitation with the card. I don’t use mushroom cards so I can’t provide any insight into why the card fails to render the template. Wild guess: the card option, where you used the template, doesn’t support templates. Although that would be surprising given that it’s called “custom:mushroom-template-card”.

EDIT

Apparently primary does support templates. :man_shrugging:

It does, but now what I’m struggling with is adding another template in the secondary containing the time in a hh.mm (am/pm) format.
I’ve been trying state: {‘sunrise.time’} and various connotations of parens/quotes without luck.

That’s an invalid template. What do you want to display?

I’m get that it’s invalid. :smiley: I’m struggling with the output format.

I’m trying to get the value of (‘sun.sun’, ‘next_rising’) in a hh:mm in a 12 hr) format.

I’m still pretty new to yaml and home assistant as a whole, so I’m still learning here.

{{ (state_attr('sun.sun', 'next_rising')|as_datetime|as_local).strftime('%-I:%M') }}

Thank you for all your help! It looks like that’s all Python. Good to know for future reference.

1 Like