HaoleBoy
(Jay)
December 26, 2023, 10:14pm
1
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’)
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"}'
OzGav
(Oz Gav)
December 27, 2023, 12:00am
2
It would appear there is something wrong with your entity. Does it exist? Is there a typo?
OzGav
(Oz Gav)
December 27, 2023, 12:07am
3
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
HaoleBoy
(Jay)
December 27, 2023, 12:16am
4
yes it exists, I actually reference it on my main screen…
HaoleBoy
(Jay)
December 27, 2023, 1:23am
5
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";
}
}
HaoleBoy
(Jay)
December 27, 2023, 1:28am
6
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";
}
}
HaoleBoy
(Jay)
December 27, 2023, 1:45am
7
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"}'
OzGav
(Oz Gav)
December 27, 2023, 1:47am
8
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"}'
OzGav
(Oz Gav)
December 27, 2023, 1:51am
9
You can simplify that by doing something like
service_data: '${(["off", "unavailable", "idle", "paused"].includes(entity.state)) ? "media_player-off" : "media_player-on"}’
HaoleBoy
(Jay)
December 27, 2023, 2:17am
10
that’s what happens when you stare at this stuff too long!
HaoleBoy
(Jay)
January 9, 2024, 3:23am
11
OzGav:
service_data: '${(["off", "unavailable", "idle", "paused"].includes(entity.state)) ? "media_player-off" : "media_player-on"}’
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,
OzGav
(Oz Gav)
January 9, 2024, 5:23am
12
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.