Combination of conditions - feeling lost

Greetings all,

I need some help concerning the structure of conditions.

The fist block is working, but I don’t know why. Ok, it seems to start with a condition, then the type of the condition (and), followed by a keyword indicating that more conditions have to follow and then the first condition is there, but there is a leading ‘-’. Why is no leading ‘-’ before the second condition?

  condition:
    - condition: and
      conditions:
        - condition: time
          after: "21:00"
          before: 05:00
        - type: is_plugged_in
          condition: device
          device_id: 41430627056111eb403adf3fd0c80
          entity_id: binary_sensor.sm_g965f_i
          domain: binary_sensor
        - type: is_plugged_in
          condition: device
          device_id: 18bb63a0034f11eb9dd4dd2ad209
          entity_id: binary_sensor.sm_g965f
          domain: binary_sensor
  action:
    - service: script.lights_off
      data: {}

The second block is still chaotic and not working at all. Basically I tried to combine different AND-conditions.
If motion is detected, first check condition A and B and if everything is fine, do C. If not, check D and E and if this is fine, do F instead. …followed by other conditions.

 trigger:
    - platform: state
      entity_id: binary_sensor.lidl_bs_occupancy
      to: "on"
  condition: []
  action:
    - choose:
        - conditions:
            - condition: and
              conditions:
                - condition: time
                  after: "16:00"
                  before: 22:00
                - type: is_plugged_in
                - condition: device
                  device_id: 41430627056111eb803adf3fd0c80
                  entity_id: binary_sensor.sm_g965f_i
                  domain: binary_sensor
                - type: is_plugged_in
                - condition: device
                  device_id: 18bb63a0034f11eba9dd4dd2ad209
                  entity_id: binary_sensor.sm_g965f
                  domain: binary_sensor
                  sequence:
                    - service: homeassistant.turn_on
                      data:
                        brightness_pct: 100
                        xy_color:
                          - 0.612
                          - 0.374
                        entity_id: light.osram_light_strip_light
              conditions:
                  - condition: time
                    after: 02:00
                    before: 05:30

Is there any good advice, concerning how to proceed? Is there any tooling to help structuring those conditions?

Thanks a lot,
Fridolin

You believe the hyphen (-) must appear before the word condition. However, that’s only because you have noticed this convention used in many examples but it isn’t mandatory. The order of the option following the hyphen is unimportant. All of these are equivalent:

Example 1

        - condition: time
          after: "21:00"
          before: 05:00

Example 2

        - after: "21:00"
          condition: time
          before: 05:00

Example 3

        - before: 05:00
          condition: time
          after: "21:00"

This might help explain it better. It shows YAML converted to JSON. The JSON equivalent is a dict (dictionary) and the order of the dict’s keys is not critical to Home Assistant’s ability to understand them.

Here’s more of your conditions converted from YAML to JSON. As you can see, each hyphen effectively creates a separate dictionary:

1 Like

Hey @123 how do we access the tool that displays yaml as json - it’s quite handy? Or, is any online tool just as good such as https://onlineyamltools.com/convert-yaml-to-json even though it isn’t homeassistant aware?

It’s an online tool. Everything in Home Assistant is standard YAML or JSON so the online converters work well.

Unrelated to this topic but another useful online tool is JSONPathFinder. It’s useful in the situation where you want to create a sensor that reports a value that’s contained within a complex JSON payload. For example, energy, weather, even garbage-collection schedules, can be presented in a complex JSON structure. JSONPathFinder simplifies the task of identifying the correct JSON path to the key/value you want.

1 Like

Hi, yes, really useful. never thought of that, usually use the template in dev tools in Lovelace.
really goid idea
thanks

@123 thank you very much for the idea and your examples - really appreciated! This approach seems pretty useful, indeed. I’ll give it a try to first come up with a JSON structure and later convert this to yaml. Let’s see what I can do :wink:

Thanks,
Fridolin

That’s not what I suggested.

I used the JSON equivalent of YAML only to illustrate a point. There’s absolutely no need to “first come up with a JSON structure and later convert it to YAML”.