#How to read part of a value for controlling device on/off

1 messages · Page 1 of 1 (latest)

icy bison
#

Hello! I'm trying to create an automation that reads a new state (from "unavailable") and then trigger a device to turn on. It's for a Bambu Lab X1C, I'm trying to read the material in "Active tray" to then trigger an external set of fans to turn on (by turning on a z-wave plug the fans are plugged into). I have the following so far:

description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.bambu_lab_x1_carbon_active_tray
    from: unavailable
conditions:
  - condition: template
    value_template: "{{ 'PLA' or 'PETG' in sensor.bambu_lab_x1_carbon_active_tray }}"
actions:
  - type: turn_on
    device_id: a833851361929981b8de7f7921311ad9
    entity_id: 3e953955c7966607d360e8cc302c2f53
    domain: switch
mode: single```

The sensor will read "Generic PLA" or "Generic PETG" when the tray is active with these materials, so trying these 2 as a test run (it will really be for ABS/ASA). I'm sure I'm doing something wrong on the condition, but I just can't figure out the proper syntax
#

some context in HA

molten zenith
#

You're not referring to the state properly

icy bison
#

"{{ 'PLA' or 'PETG' in states('sensor.bambu_lab_x1_carbon_active_tray') }}"

#

like that?

molten zenith
#

No

#

The state reference is right bit the rest is wrong

icy bison
#

guess I'm confused on how to do a "contains" search on a state string then

#
    condition: template
    value_template: "{{ 'ABC' in trigger.to_state.state }}"``` this was an example I found that worked for someone
#

seems like "in" only returns True False

#

but should that not work for a condition?

molten zenith
#

yes, but you're trying to search for two different strings

icy bison
#

ah I can't just slap an or in there I guess

molten zenith
#

you can do something like {{ ['foo']|select('search', 'foo|bar')|list|length > 0 }}

#

Correct, you can't just slap an 'or' in there. You could use two separate clauses, one for each string, or do something like I did above

icy bison
#

so I could do 'foo' in states() or 'bar' in states()?

molten zenith
#

yes

#

that's the most straightforward way, but it's wording and repetitious

#

there's also {{ 'foo'|contains('foo') }}, but unfortunately it doesn't take a regex

icy bison
#

I would say I'm not quite advanced enough to do things like string searches, this seems easier to follow for now

molten zenith
#

sure

icy bison
#

testing it now, thanks for the help