Assign value to variable

Hi,

I’m in the process of learning ESPhome and doing my first project.
Can someone explain to me how can I do something as simple as assigning a value to a variable?
I have this:

globals:
  - id: zmiana_predkosci_mozliwa
    type: bool
    restore_value: no
    initial_value: 'false'

and then this:

sensor:
  - platform: pulse_counter
    pin: D4
    name: "Pulse Counter"
    update_interval: 5s
    unit_of_measurement: RPM
    accuracy_decimals: 0
    on_value_range:
      - below: 10
        then:
          zmiana_predkosci_mozliwa = false;
      - above: 11
        then:
          zmiana_predkosci_mozliwa = true;

That doesn’t work.

I’ve tried some weird combinations from the ESPhome docs, but so far I didn’t find any explanation on how to accomplish such a trivial thing.

Hi there,
I’m not using ESPHome but according the doc.

on_...:
  - globals.set:
      id: my_global_var
      value: '10'

so i’ll try :

on_value_range:
    - below: 10
      then:
        - globals.set:
          id: zmiana_predkosci_mozliwa
          value: false
    - above: 11
      then:
        - globals.set:
          id: zmiana_predkosci_mozliwa
          value: true

:man_shrugging:

:man_facepalming:

How could I’ve missed that.

Thank you very much.

1 Like

How can this be done with a global of type string? Any help is much appreciated.
Booleans and numbers are easy. Docs only give this simplest of examples. Sad face.

globals:
  - id: bootmsg
    type: char[10]
    initial_value: "{'b','o','o','t'}"     #  THIS WORKS.  
                                # I DON'T KNOW A BETTER WAY  

on_...:
      then:
        - globals.set:
          id: bootmsg
          value: ???     #  WHAT GOES THERE?
                         # NOTHING I TRY WORKS

I used the text_sensor component:

text_sensor:
  - platform: template
    id: bootmsg

on_...:
      then:
        - lambda: 'id(bootmsg).publish_state("boot");'

Then you can access the value with:

!lambda "return id(bootmsg).state;"

Thanks for a simpler way to update text messages on my OLED display. The text sensor is easier and smaller to use than a global string for the text message. Using %s and id(displaytext).state.c_str() as below:

text_sensor:
  - platform: template     
    id: displaytext      
 
on_...:
      then:
        - lambda: 'id(displaytext).publish_state("boot");'

display:                             
  - platform: ssd1306_i2c                
    model: "SSD1306 128x64"
#    reset_pin: D0
    address: 0x3C            
    lambda: |-
      it.printf(0, 40, id(myfont14), "message: %s", id(displaytext).state.c_str());   

BUT, the question was how do I update the value of a global string of type char[10] with global.set
No working solution of any kind, yet.

I did figure out how to do this with a global string of type: std::string
As long as both displaymessage and displaymessage2 are of the same type: std::string I can do this:

on_...:
      - globals.set:
          id: displaymessage
          value: id(displaymessage2).c_str()     

globals:
  - id: displaymessage
    type: std::string 
    initial_value: "{'w','i','f','i',' ','c','o','n','n'}"   

  - id: displaymessage2
    type: std::string 
    initial_value: "{'W','I','F','I',' ','C','O','N','N'}"     

I also tried to assign the text sensor state (after publish) to a global string type: std::string using global.set with no luck. The output is just blank. And with char[10] it won’t even compile.
id(displaytext).state.c_str()
id(displaytext).state

on_...:
      - lambda: 'id(displaytext).publish_state("bOOt");'     
      - globals.set:
          id: displaymessage
          value: id(displaymessage2).c_str()    #  THIS WORKS  
#          value: id(displaytext).state.c_str()  # DISPLAYS NOTHING
#          value: id(displaytext).state  # DISPLAYS NOTHING

display:                                
  - platform: ssd1306_i2c                
    model: "SSD1306 128x64"
#    reset_pin: D0
    address: 0x3C          
    lambda: |-
      it.printf(0, 0, id(myfont14), "status: %s", id(displaymessage).c_str());   

At least I now have 1 string solution, and 1 using a text sensor. THANKS