What is wrong in this template?

Can somebody tell me why this template doesn’t work?

 - variables:
      person: person.fha
      bg: |
        [[[
          if (states['person.fha'].state == 'home' )
            return 'url(/local/lovelace/images/Casa.png)';
          if (states['person.fha'].state == 'Vino Di Sup' )
            return 'url(/local/lovelace/images/vino_di_sup.png)';
          if (states['person.fha'].state == 'Maxi Tigre' )
            return 'url(/local/lovelace/images/Maxi_Tigre.png)';
          if (states['person.fha'].state == 'Tody's' )';
            return 'url(/local/lovelace/images/Todys.png)';
          return 'url(/local/lovelace/images/Fha_away.jpg)'
        ]]]

Try putting ‘else’ in front of the last return and also in front of every ‘if’ after the first one.

This way:

 - variables:
      person: person.fha
      bg: |
        [[[
          if (states['person.fha'].state == 'home' )
            return 'url(/local/lovelace/images/Casa.png)';
          else if (states['person.fha'].state == 'Vino Di Sup' )
            return 'url(/local/lovelace/images/vino_di_sup.png)';
          else if (states['person.fha'].state == 'Maxi Tigre' )
            return 'url(/local/lovelace/images/Maxi_Tigre.png)';
          else if (states['person.fha'].state == 'Todis' )';
            return 'url(/local/lovelace/images/Todys.png)';
          else return 'url(/local/lovelace/images/Fha_away.jpg)'
        ]]]

EDIT: no, it still doesn’t work

states['person.fha'].state

I think you mean:

states('person.fha').state

( ) and not [ ]

Assuming of course person.fha is some entity. I also cannot see why you have a variable.

I read that using the custom button-card i must use [ ].
and those variables are used by custom button card.
I noticed that if i use only these lines it works…

          if (states['person.fha'].state == 'home' )
            return 'url(/local/lovelace/images/Casa.png)';
          if (states['person.fha'].state == 'Vino Di Sup' )
            return 'url(/local/lovelace/images/vino_di_sup.png)';
          return 'url(/local/lovelace/images/Fha_away.jpg)'

If i add more lines it stops working.

If this is custom button then post everything in the entire button_card_template.
Because at the very least variables doesn not need a “-” and you need triple [[[ ]]]].

Like this for example:

button_card_templates:
  patio_vizio:
    triggers_update: all
    variables:
      tv: patio_vizio
      ip: >-
        [[[ return states['sensor.vizio_tvs'].attributes.tvs.find(x=>x.name ==
        'patio_vizio').ip ]]]
      port: >-
        [[[ return states['sensor.vizio_tvs'].attributes.tvs.find(x=>x.name ==
        'patio_vizio').port ]]]
      auth: >-
        [[[ return states['sensor.vizio_tvs'].attributes.tvs.find(x=>x.name ==
        'patio_vizio').auth ]]]
      clientaddr: >-
        [[[ return states['sensor.vizio_tvs'].attributes.tvs.find(x=>x.name ==
        'patio_vizio').clientAddr ]]]

Ok, i’l try it… Thanks.

That is important information you should have included in your original post. We’re not privy to your full configuration or mind readers.

Also choosing the correct category would have helped slightly (Configuration Frontend == dashboards and cards).

1 Like

Extra quote mark in your second post; two in your first with the apostrophe as well:

          else if (states['person.fha'].state == 'Todis' )';
----------------------------------------------------------^
            return 'url(/local/lovelace/images/Todys.png)';
          else return 'url(/local/lovelace/images/Fha_away.jpg)'
        ]]]

(edit: removed potentially-confusing response to kbrown01’s comment)

You’re mixing up jinja templates and JS templates. JS templates do not use anything related to Jinja.

Typically, all of the JS templates require [] only and () do not work at all. This is true for custom:button-card and custom:config-template-card

1 Like

Yes, now i understand…