How to set state value with a template

Hi all
I cannot seem to assign a value to “value:” in my code below. I have tried various options with no luck. Could someone please assist?
Thanks in advance.

The entity “sensor.alarm_zone_06” value = “06;Y;Y;Y;ZoneName”. Therefore I want the icon to display red if value = ‘Y’


- type: custom:button-card
        entity: sensor.alarm_zone_06
        name: >-
          [[[ return states["sensor.alarm_zone_06"].state.split(";")[0] + " " +
          states["sensor.alarm_zone_06"].state.split(";")[4] ]]]
        icon: mdi:bell-outline
        size: 25px
        state:
          - value: '{{ states.sensor.alarm_zone_06.state.split(";")[1] }}'
            color: red
            icon: mdi:bell-ring-outline
            styles:
              card:
                - animation: blink 2s ease infinite
          - operator: default
            color: lime

I have also tried:

      - type: custom:button-card
        entity: sensor.alarm_zone_06
        name: >-
          [[[ return states["sensor.alarm_zone_06"].state.split(";")[0] + " " +
          states["sensor.alarm_zone_06"].state.split(";")[4] ]]]
        icon: mdi:bell-outline
        size: 25px
        value_template: >- {% if is_state('sensor.alarm_zone_06.state.split(";")[1]', 'Y') %}
                             'Y'
                           {% endif %}
        state:
          - value: 'Y'
            color: red
            icon: mdi:bell-ring-outline
            styles:
              card:
                - animation: blink 2s ease infinite
          - operator: default
            color: lime


You can’t use Jinja templating in Custom Button cards, you must use the Javascript-style templates:

type: custom:button-card
entity: sensor.alarm_zone_06
name: |-
  [[[ return states["sensor.alarm_zone_06"].state.split(";")[0] + " " +
  states["sensor.alarm_zone_06"].state.split(";")[4] ]]]
icon: mdi:bell-outline
size: 25px
state:
  - operator: template
    value: |-
      [[[ return states["sensor.alarm_zone_06"].state.split(";")[1] === "Y" ]]]
    color: red
    icon: mdi:bell-ring-outline
    styles:
      card:
        - animation: blink 2s ease infinite
  - operator: default
    color: lime

Thank you so much! It works perfectly :smiley:

1 Like