Mushroom styling with template

I managed to get this code

{% set starton = (states('sensor.luz_pasillo_off_on') | as_timestamp | int) %}
{% set diferencia = ((as_timestamp(now()) |int) - (starton)) %}
{% if is_state('switch.ladron_pasillo_0_enchufe_1', 'on') and diferencia <= 2 %}
--shape-animation: blink-fast 0.5s infinite;
{% elif is_state('switch.ladron_pasillo_0_enchufe_1', 'on') and diferencia > 2 <= 4 %}
--shape-animation: blink-medium 1.5s infinite;
{% elif is_state('switch.ladron_pasillo_0_enchufe_1', 'on') and diferencia > 4 <= 6 %}
--shape-animation: blink-slow 2.5s infinite;
{% endif %}  

sensor.luz_pasillo_off_on gives time from last change from off to on

so the idea is to change blinking rate across time.

I have achieved the shape to blink, but only fast, it keeps blinking infinite
event though I want

  • fast between 0-2 seconds
  • medium between 2-4
  • slow between 4-6

I think something with the time comparison is happening but don´t know what, any help please?

This will never work because the template does not update fast enough.

That template will only update when switch.ladron_pasillo_0_enchufe_1 and sensor.luz_pasillo_off_on changes state. It also will only update every time 1 minute passes.

Your logic is looking for updates less than 6 seconds.

The only way to make this work is to have a trigger based template sensor update once a second with the same logic you have above.

Hi thanks for your reply.
I am always making things difficult, and just for style.

So no way, unless the trigger template, and will this cause problems to the system? cause every one second seems too much work, isn´t it?

what kind of template will it be?

I don’t think it will cause any problems if you have a good PC for your server. The template will just trigger every second.

I am getting blinking durations that exceed what I expected.

Are you using a trigger based template sensor? Because it doesn’t look like you are. It looks like you just made a template sensor with the same template.

To be clear, if you don’t specify a time_pattern trigger anywhere in your configuration, your template will not rerender every second.

template:
- trigger:
  - trigger: time_pattern
    seconds: "/1"
  sensor:
  - name: jsdklfjaslkdj
    unique_id: jsdklfjaslkdj
    state: > 
      {% set starton = (states('sensor.luz_pasillo_off_on') | as_timestamp | int) %}
      {% set diferencia = ((as_timestamp(now()) |int) - (starton)) %}
      {% if is_state('switch.ladron_pasillo_0_enchufe_1', 'on') and diferencia <= 2 %}
      --shape-animation: blink-fast 0.5s infinite;
      {% elif is_state('switch.ladron_pasillo_0_enchufe_1', 'on') and diferencia > 2 <= 4 %}
      --shape-animation: blink-medium 1.5s infinite;
      {% elif is_state('switch.ladron_pasillo_0_enchufe_1', 'on') and diferencia > 4 <= 6 %}
      --shape-animation: blink-slow 2.5s infinite;
      {% endif %}

This cannot be done from the UI. This can only be done from configuration.yaml

tha`s the key heheh.

I have added the code to configuration.yaml, but duration, but for the first 2 seconds, don´t work either, also the slow blinking never happens

Screenshot_20241025_161622

thank you

You have to alter your template to provide a value for an else condition. Otherwise it will be unavailable.

I also suggest you post the states for all sensors in your template. Even the template for the entity sensor.luz_pasillo_off_on, because your template seems odd.