Repeat something?

Hi,
I want to have a repeated change of text colors until value of HA sensor is above 800. I tried to that this way but I get only one change of colors instead all the time until value goes below 800.

- platform: homeassistant
    id: Co2
    entity_id: sensor.co2_dnevna_dnevna_co2    
    on_value:      
      - lvgl.label.update:
          id: co3_text_out
          text:
            format: "%.0f"
            args: [ 'x' ]
    on_raw_value: 
      - while: 
              condition:
                lambda: |-
                  return id(co3_text_out).state > 800;
              then:
                - lvgl.widget.update:
                    id: co3_text_out
                    text_color: black
                - delay: 500ms
                - lvgl.widget.update:
                    id: co3_text_out
                    text_color: white

Shouldn’t that be (co2).state ?
And your description is not so clear if you want to change color while >800 or <800.
You could do the color change while loop in a script and execute the script from on_value.

1 Like

I think you also need to put a second delay after the white update (or alternatively before the black update.

Because as you have it the white doesn’t show for any duration.

And like Karosm says you probably want to refer to the sensor value, which you can also do with the shorthand x.

@Karosm
I have a typo when wrote this and let it stay like Co3 :slight_smile:
I want to colors start to change when CO2 goes above 800 and stop changing colors when it goes below 800.
If I know how to make script to have non stop color change, I wouldn’t ask here :grin:

Can you please explain better? I don’t know how to do that

try something like this, it also shows how to use x on ha sensor.

# ha sensor automation 
  on_value:
    - lvgl.label.update:
        id: co3_text_out
        text:
          format: "%.0f"
          args: [ 'x' ]

    - if:
        condition:
          lambda: |-
            return x > 800;
        then:
          - script.execute: blink_co2
        else:
          - script.stop: blink_co2
          - lvgl.widget.update:
              id: co3_text_out
              text_color: white

# script component
script:
  - id: blink_co2
    mode: restart
    then:
      - while:
          condition:
            lambda: |-
              return id(Co2).state > 800;
          then:
            - lvgl.widget.update:
                id: co3_text_out
                text_color: black
            - delay: 500ms
            - lvgl.widget.update:
                id: co3_text_out
                text_color: white
            - delay: 500ms

Not tested, but you get idea.

1 Like

I linked you to the docs which has this example.

sensor:
  - platform: dht
    # ...
    on_raw_value:
      then:
        - light.turn_on:
            id: light_1
            red: !lambda "return x/255;"

2 Likes

@karosm

It works! Thanks!! :fireworks:

You are welcome!