Get state (on/off , enabled/disabled) of an AUTOMATION

  • Installation method - Home Assistant OS
  • Core - 2025.9.3
  • Supervisor - 2025.09.0
  • Operating System - 16.2
  • Frontend - 20250903.5

Setting up a mushroom-template-card as a button to enable/disable an automation that turns on/off the light in my office. Our frequent visitors, including my sleep deprived daughter with her newborn, don’t appreciate the light turning on when they come into the room. I already have time constraints and have told them where the motion sensor is so they can just turn it around, but that’s a discussion for a different day.

ANYWAY - I want to change the icon, icon color and secondary line on the card based on the AUTOMATION being on/off (state). I can’t figure out how to get the actual state. Here’s what I’ve tried in the template editor:

ONE: {{ states.automation.motion_activated_light_with_time_office }}

TWO: {{ state_attr( 'automation.motion_activated_light_with_time_office', 'state') }}

THREE: {% if is_state('automation.motion_activated_light_with_time_office', 'on') %}
    On
{% elif is_state('automation.motion_activated_light_with_time_office', 'off') %}
    Off
{% endif %}

FOUR: {{ state_attr( 'automation.motion_activated_light_with_time_office', 'template') }}

FIVE: {{ state_attr( 'automation.motion_activated_light_with_time_office', 'TemplateState') }}

ID: {{ state_attr( 'automation.motion_activated_light_with_time_office', 'id') }}
LAST_TRIGGERED: {{ state_attr( 'automation.motion_activated_light_with_time_office', 'last_triggered') }}
MODE: {{ state_attr( 'automation.motion_activated_light_with_time_office', 'mode') }}
CURRENT: {{ state_attr( 'automation.motion_activated_light_with_time_office', 'current') }}
FRIENDLY_NAME: {{ state_attr( 'automation.motion_activated_light_with_time_office', 'friendly_name') }}

and here are the results:

ONE: <template TemplateState(<state automation.motion_activated_light_with_time_office=off; id=1757439227814, last_triggered=2025-09-15T15:28:31.506019-04:00, mode=restart, current=0, friendly_name=Motion-activated Light with Time Office @ 2025-09-15T15:29:10.212808-04:00>)>

TWO: None

THREE: 
    Off


FOUR: None

FIVE: None

ID: 1757439227814
LAST_TRIGGERED: 2025-09-15 19:28:31.506019+00:00
MODE: restart
CURRENT: 0
FRIENDLY_NAME: Motion-activated Light with Time Office

It’s right there! Only way I have it working is repetitively putting the %IF into the yaml.

Here’s the button yaml:

type: custom:mushroom-template-card
entity: automation.motion_activated_light_with_time_office
grid_options:
  columns: 3
  rows: 2
tap_action:
  action: perform-action
  perform_action: automation.toggle
  target:
    entity_id: automation.motion_activated_light_with_time_office
  data: {}
hold_action:
  action: none
icon: >-
  {% if is_state('automation.motion_activated_light_with_time_office', 'on') %}
    mdi:motion-sensor
  {% elif is_state('automation.motion_activated_light_with_time_office', 'off')
  %}
    mdi:motion-sensor-off
  {% endif %}
double_tap_action:
  action: none
color: >-
  {% if is_state('automation.motion_activated_light_with_time_office', 'on') %}
    green
  {% elif is_state('automation.motion_activated_light_with_time_office', 'off')
  %}
    black
  {% endif %}
vertical: true
features_position: bottom
primary: Motion Sensor
secondary: >-
  {% if is_state('automation.motion_activated_light_with_time_office', 'on') %}
    On
  {% elif is_state('automation.motion_activated_light_with_time_office', 'off')
  %}
    Off
  {% endif %}
multiline_secondary: false

I mean, it works, but seems a bit kludgy. Any thoughts or suggestions?

Appreciate it.

Mushroom template cards support a keyword entity that will render whatever entity ID you use in the entity configuration variable. So, templates that take an entity ID as an argument can be simplified significantly:

{{ states(entity) }}
{{ is_state(entity, 'on') }}

Combining those with in-line if’s in your expressions makes a more compact and readable config.

type: custom:mushroom-template-card
entity: automation.motion_activated_light_with_time_office
grid_options:
  columns: 3
  rows: 2
tap_action:
  action: perform-action
  perform_action: automation.toggle
  target:
    entity_id: automation.motion_activated_light_with_time_office
  data: {}
hold_action:
  action: none
icon: >-
  {{'mdi:motion-sensor' if is_state(entity, 'on') else 'mdi:motion-sensor-off'}}
double_tap_action:
  action: none
color: "{{'green' if is_state(entity, 'on') else 'black'}}"
vertical: true
features_position: bottom
primary: Motion Sensor
secondary: "{{states(entity)|capitalize}}"
multiline_secondary: false

sunuvabiscuit, I’m pretty sure I tried

{{ states( entity) }}

but apparently HA is proving me wrong. That does work and provides the expected ‘on’ and ‘off’ values.

I’m glad my coffee coaster doubles as a pad when I bang my head on the desk.

Also, agree with the inline IF statements. Was bludgeoning my way through the code as it’s been about 15 years since I wrote daily.

Thanks for the tips!