Try to get started programming but steep learning

Hi,
I used to program in the past and want to be able to witte my own programs/scripts.
I try to get started with something simple.
I creates a simple switch called thuis
I can show the status in the markup card
Using snarky’s VAR integration I could create a variable X by adding this to the configuration.yaml
What I want is every time I set the switch on is to increase X with 1 and dispaly that in de marup card

I have no clue where to start with this.
I guess I need to wriste a script for that and I tried:
‘’’
alias: test varx
description: “”
trigger:

  • platform: state
    entity_id:
    • input_boolean.afwezig
      from: “off”
      to: “on”
      condition: []
      action:
  • service: notify.persistent_notification
    data:
    message: weggegaan
    X=X+1
    mode: single
    ‘’’
    But I see nothing happening and when I reload the YAML code that X=X+1 is removed

For starters, please use proper formatting by using this button:image
if not, it’s going to be difficult to see what you’re trying to do, as yaml is very strict in indentation :wink:

Also this is not a community guide (please read the category descriptions). Moved.

I thought this would be a poprer place I created a new topic in community guides with I hope proper formatting

tried to poste the code with copy /paste and added ‘’’ chars before and afters sorry that I do not understand all the buttons I am new here

type or paste code here

Just hit the button, and replace ‘type or paste code here’ with your own code.

Anyway, instead of using a custom integration, it would be easier to use a number helper, and increase that value :wink:

This is being discussed in this other topic:

https://community.home-assistant.io/t/try-to-get-started-programming-but-steep-learning-cruve/526252

Hi,
I hope I have the proper topic and formatting now after posting in a wrong topic. If it is moved by a moderator please inform me where it goes?
I used to do some programming in the past in Pascal and *051 assembler and I want to be able to witte my own programs/scripts.
I try to get started with something simple.
I created a simple switch called thuis
I can already show the statusof that switch in the markup card
Using snarky’s VAR integration I could create a variable X by adding this to the configuration.yaml And I also can see the value of X in markup
What I want is every time I set the switch from of to on, to increase X with 1 and dispaly that in de marup card

I have no clue where to start with this.
I guess I need to write a script for that, and I tried this code

alias: test varx
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.afwezig
    from: "off"
    to: "on"
condition: []
action:
  - service: notify.persistent_notification
    data:
      message: weggegaan
    X=X+1
mode: single

I don’t have experience with that integration you used to create a variable, but anyways, I would suggest using the native counter helper instead.
I’m other to do that, go to Settings > Devices and Services, select the tab Helpers and add a helper, selecting Counter as the type you want and following the required steps over there.

After creating the counter, use the counter.increment service in your automation.

Something like this:

alias: test varx
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.afwezig
    from: "off"
    to: "on"
condition: []
action:
  - service: counter.increment
    data: {}
    target:
      entity_id: counter.your_new_counter
  - service: notify.persistent_notification
    data:
      message: "Counter = {{ states('counter.your_new_counter')}}"
mode: single

But if you still want to use that integration, then it will be probably something like this (based on their docs, I’ve never used that as I mentioned:

alias: test varx
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.afwezig
    from: "off"
    to: "on"
condition: []
action:
  - service: var.set
    data_template:
      entity_id: var.x
      value: "{{ states('var.x') | float(0) + 1.0}}"
  - service: notify.persistent_notification
    data:
      message: "Counter = {{ states('var.x')}}"
mode: single
1 Like

Hi Thanks for your feedback.
This counter was just a simple example for testing that I canchage a vatiable after a trigger.
But this enlightens me a lot.
A thing that is simple in programming languages is quite different if you use YAML
At least I now know how much one must type to do such a simple thing like changing a variable a a trigger;)
Just used to a more ‘procedure’‘or function’ call system like I used to build old time state/event engines

BTW the code works;)

As your goal is to learn, there’s another way to solve this which I probably will choose as it concentrates all the settings in a single entity and will probably be easier to maintain, which is using a template sensor.

Something like this:

template:
  - trigger:
      - platform: state
        entity_id:
          - input_boolean.afwezig
        from: "off"
        to: "on"
    sensor:
      - name: Variable X
        unique_id: any_unique_id_for_variable_x
        state: "{{ states('sensor.variable_x') | float(0) + 1.0 }}"
1 Like

Merged into this topic. Thanks for the heads up.

1 Like

Another option for this is to use a History Stats sensor (though this isn’t strictly limited to state changes from ‘off’ to ‘on’):

sensor:
  - platform: history_stats
    name: Afwezig On Count
    entity_id: input_boolean.afwezig
    state: "on"
    type: count
    start: "{{ 0 }}"
    end: "{{ now() }}"
2 Likes

Yaml (Yet another markup language) is not a programming language at all, but a way of ordering, grouping and storing configuration lines (markup).
The use of templates comes closer and uses Jinja, but it still ain’t a programming language.

HA itself is written in Python, and by the sound of it, you want to have more of a ‘programming’ environment.

For that i think AppDeamon, or Pyscript, come closer, or when you are more in to Java, try NodeRed. Node Ref also takes a while to get used to, but it does enable you to use plain java when needed.
The is also a c# version around somewhere, but can’t find the link right now :yum:

1 Like

Just a quick FYI. You should probably use this.state in the template instead of getting the value via the states function.

3 Likes

Thnaks all for the info. This sure helps me further.
For programming I think I will take the Java or python way.:slight_smile:
I will look which one I like most, but think the more formal Java might win but I will try some pythong to.
I found a nice turorial on python on W3

1 Like