Floor plan Entity-state related class

I’m trying to set a SVG element class based on the entity state.
this example is in the usage doc

- entities:
    - binary_sensor.kitchen
    - binary_sensor.laundry
  state_action:
    action: call-service
    service: floorplan.class_set
    service_data:
      class: '${(entity.state === "on") ? "motion-on" : "motion-off"}'

I’m using it in my startup action, like this

  startup_action:
    - service: floorplan.class_set
      service_data:
        entity: sensor.thermostat_ground_action
        element: heat-button-highlight
        class: '${(entity.state === "heating") ? "button-on" : "button-off"}'

I get this error
PE ERROR Cannot read properties of undefined (reading ‘state’)

undefined

If I replace the class like below it works, I’m not getting what I’m doing wrong.

class: button-off

so obviously it doesn’t like this:

class: '${(entity.state === "heating") ? "button-on" : "button-off"}'

It would appear there is something wrong with your entity. Does it exist? Is there a typo?

I don’t use start-up action other than in multi layer floorplans. I do notice in the examples that the class_set is done on elements when you are trying to reference an entity. Also I don’t believe this is even required as when the floorplan starts up it will go through and run all the rules so anything based on an entity state will get set correctly…

Edit: if you must access the properties of an entity you can always do it using the hass entity. See here for examples Floorplan does not detect changes in cover.* entities to "set class"? - #7 by Aephir

yes it exists, I actually reference it on my main screen…


In the post you shared, I don’t see he got it resolved with that code.

regardless of the state, it always turns on

    - element: fan-button-on-highlight
      state_action:
        - service: floorplan.class_set
          service_data:
            class: |-
              {
                const fan_mode = hass.states["sensor.ground_therm_fan"].state;
                if (fan_mode === "on") {
                 return "button-on";
                } else {
                  return "button-off";
                }
              }

it seems like it does not evaluate the if statement.

I tried to break it and it sill turn it on. only way i could break it was to mess up the element name

    - element: fan-button-auto-highlight
      state_action:
        - service: floorplan.class_set
          service_data:
            class: |-
              {
                const fan_mode = hass.states["sensor.ground_therm_fan"].state;
                if (fan_mode === "xx") {
                 return "xx";
                } else {
                  return "xx";
                }
              }

after playing around with it. This works.

in case others need this for reference.

    - entity: sensor.ground_therm_fan
      element: fan-button-on-highlight
      state_action:
        - service: floorplan.class_set
          service_data: ${(entity.state === "on") ? "button-on" :"button-off"}
    - entity: sensor.ground_therm_fan
      element: fan-button-auto-highlight
      state_action:
        - service: floorplan.class_set
          service_data: '${(entity.state === "auto") ? "button-on" : "button-off"}'

That is because you haven’t put in the rule what entity to listen to for state changes. If you are confused by that statement compared to earlier, normal rules and startup actions are different. At startup no entity is monitored because you are just setting the various elements.

    - entity: sensor.ground_therm_fan 
      element: fan-button-auto-highlight
      state_action:
        - service: floorplan.class_set
          service_data: service_data: '${ entity.state === "on" ? "some-class-on" : "some-class--off"}'

You can simplify that by doing something like

service_data: '${(["off", "unavailable", "idle", "paused"].includes(entity.state)) ? "media_player-off" : "media_player-on"}’

that’s what happens when you stare at this stuff too long!

how can use this if there are 3 possible states on, off and unavailable? 3 classes are binary-sensor-on, binary-sensor-off, binary-sensor-unavailable,

You can nest the statement

service_data: '${(entity.state==“off”)? “binary-sensor-off” : (entity.state==“on”)? “binary-sensor-on” : “binary-sensor-unavailable”}

I just did the quickly on my iPad so hopefully no typos but I think you get the idea.