Raspberry, many Arduinos and all i can do with it

about two years ago i started to get interested in electronic parts.
i bought my first Arduino (UNO) and started collection parts i thought were interesting to experiment with.
at first it was very simple. i still have a wooden box with a small nokia screen which shows the temperature.
but the collection grew fast.

the programming language from arduino was the first new language i learned since a long time.
a bought a few 433 mhz parts and started to let the arduinos communicate with each other.

I found out that i could use the arduino to turn on/off the many old switches i have laying around.
but although that was nice, it needed an envirement on the PC to make it work.

So (without thinking about other options that were available on internet) I started to create a programm in Excel (VBA) to communicate with my switches and to save the temperatures send from the arduinos.

that was nice for a while. I even created an app to use the switches.
There was no real need to change everything again, but the PC needed to stay alive all the time.
and that was a little annoying.

So i bought a raspberry pi.
and then i really needed to learn a lot. I started to reproduce the envirement i had created in Excel, in python with Tkinter.
it took me a while and i still think off myselve as a noob in python, but it was all functioning.

then, by accident, i bouth a few small transceivers called nRF24L01+.and when i started to search for that in combination with arduino i stumbled on Mysensors.

well done Rene! everything i had done before was presented there in a very simple way.
on the website from Mysensors i selected HA as my controller and all my old programming is now in a map called old.

i still need to install everything on the raspberry, and again is the PC 24/7 on.

in the moment runs HA with 11 old switches, which are switched trough an arduino with an 433 mhz transmitter.
the switches are placed outside in my garden, upstairs in the sleepingroom, here in the livingroom, in the diningroom, and by my aquariums in the basement.
5 arduinos send temperature from 3 places outside (including the temp from my pond) and 4 places inside.
they also send lightlevel and some movement.
the fritzbox tells HA when my wife is home to switch the lights on.

already i have 5 more arduinos here to automate and collect much more data.

plans i have for the near future:

  • read the humidity from my vegatable garden. (parts are already there)
  • irrigate automaticly when the soil is dry. (parts already there)
  • read temperature from my Aquariums (parts already there)
  • read temperature on lot more places
  • motion detection on several places (parts already there)
  • make warm water collector to save power.
  • use temperature inside and outside to cool down or warm the nside off the house
  • many more things i can think about.

I must say i am really glad with the latest update. to be able to see the history and manipulate it in any way i want was 1 of the biggest wishes i had from HA.

and to see that HA is developing in a way i like made my decide to stay here and register.

a few things that i hope will be developed soon:

  • an easy way to change the frontends appearance
  • a better way to communicate with the mysensor 433 mhz switches. it would be nice if i could send a code to a switch. (if it is possible i havent found it yet.

I hope my story can inspire others.
and i hope that in the future i will be able to programm good enough to develope some small parts myselve.

3 Likes

Regarding sending a code, you should be able to use the IR switch service:

yeah, i have seen that part.
and i found out how to make it work out off the services part.

but i dont know how to use it from out of configuration.

would be nice if i could write something like:

sensor.arduino2_3_0: # the 433 mhz sensor
  friendly_name: switch 1
  data:
    V_IR_SEND: 'KP01'  # the code to switch kaku P01
sensor.arduino2_3_0 2: # the 433 mhz sensor
  friendly_name: switch 2
  data:
    V_IR_SEND: 'EA01'  # the code to switch elro A01

and that would show in the frontend as multiple switches.
i cant find something like an IR remote control in the frontend, when there is something like that i can rewrite it to make an 433 mhz remote control.

The mysensors IR switch will be shown as a regular switch in the frontend. You have to write an automation to change the code. You should be able to use an input select entity to be able to configure it manually via the frontend.

For your example, first make sure you setup your sketch to use the correct S and V types. See the example sketch in the docs I linked. Then find your IR switch entity_id in the developer tools. I would try a template switch to achieve what you want. Then you can use the same IR switch entity to send the actual code but create different template switches that set different codes when using the service.

1 Like

i will take another look at it.
when i get it working i will post the general code parts here.

if you say its possible, i will make it work :wink:

1 Like

oke, finally i understand the templateswitches. (or at least a part from it)
and i made a templateswitch like this:

 switch:
   platform: template
   switches:
     roomlight:
       friendly_name: roomlight template
       value_template: '{{ states.switch.afstandbediening_2_204.state }}'
       turn_on:
         service: switch.mysensors_send_ir_code
         entity_id: switch.afstandbediening_2_204
         data:
           V_IR_SEND: 'P01t'  
       turn_off:
         service: switch.turn_off
         entity_id: switch.afstandbediening_2_204
         data:
           V_IR_SEND: 'P01f'
  

and i made an arduino who can understand that. but the switch turns on and then instandly off again.
so i must make 2 switches for every switch. thats annoying.

Please edit your post and add code block markdown tags around the config, to keep formatting info.

You can turn off the automatic off-switching by changing optimistic to false in your mysensors config, and provide your own state feedback for all switches/lights instead.

i changed optimistic to false but still it switches of directly after it is switched on.

isnt that something in the service?

(i hope i have edited it allright so)

Not quite, it should be three back-tics, newline, code, newline, three back-tics, to show as code block, in the forum.

What does your receive or incomingMessage function hold in the sketch? Can you post that part?

and so i must keep on learning.
what do you mean with back-tics, i’m not familair with that term.

the incomingmessage checks which sensor, if the messagetype is IR_SEND and then it splits the message.
im still struggling with the code because i had to learn to many new languages in a short amount of time and i dont want to post more code here before i know how to do it right :wink:

but i understand why you asked.
to make sure that nothing is coming back from the arduino, i made the void, completely empty. so except the sleep in de mainloop is there nothing after the setup.
still the switch sets back to the offstate after it was turned on.
so i must change something in HA.

This is a backtic: `
I think it’s called accent grave in french.
You can also use it to get a character like this: è
I press the backtic key on my keyboard and then the space key, to get the backtic on its own.

You have to send the state feedback in your sketch if optimistic is false. Otherwise the switch will go back to previous state, because no state feedback was received after home assistant sent out the message to change state. This setting is useful to avoid changing state falsely if a message is lost in transit. If you would set optimistic to true, the IR switch would automatically turn off after being turned on. This is a special behavior for the IR switch, to make it behave as a push button. Normal switches keep the last state change if optimistic is true, and don’t wait for state feedback.

So no, you don’t have to change something in HA. You have to make sure your sketch is sending back the correct values after state change. See example of incomingMessage function below:

void incomingMessage(const MyMessage &message) {
  if (message.type==V_LIGHT) {
    // IR sender not implemented, just a dummy print.
    if (message.getBool()) {
      Serial.print("Sending code ");
      Serial.println(code);
    }
    gw.send(msgSendCode.set(message.getBool() ? 1 : 0));
  }
  if (message.type == V_IR_SEND) {
    // Retrieve the IR code value from the incoming message.
    String codestring = message.getString();
    codestring.toCharArray(code, sizeof(code));
    Serial.print("Changing code to ");
    Serial.println(code);
    gw.send(msgCode.set(code));
  }
}

Modify and/or add you own logic as needed, eg splitting the message, but make sure you send back the updated state, like in my example above.

got it with the backtics.
thx for your patience.

tonight whe i got home i saw what you mean about the optimistic setting. now all my switches flip back :wink:
im going to work with this info, tonight or tommorow, but you made me a bit smarter again :wink:

1 Like

I am afraid this ist gonna work.
the arduino makes 1 switch and that switch is showing up in the frontend.
that 1 switch is actually up to a 1000 different switches.

if i make a templateswitch based on the 1 switch, then that switch is changed with the templateswitch.
if i make a second templateswitch, that also is connected to the baseswitch.

i took the IR sketch and only changed the childid, the sketch info and the automatic turn of, then i put this code in the configuration:

switch:
  platform: template
  switches:
    diningroomlight:
      friendly_name: dining room light template
      value_template: '{{ states.switch.afstandbediening_2_205.state }}'
      turn_on:
        service: switch.mysensors_send_ir_code
        entity_id: switch.afstandbediening_2_205
        data:
          V_IR_SEND: 'P01t'
      turn_off:
        service: switch.mysensors_send_ir_code
        entity_id: switch.afstandbediening_2_205
        data:
          V_IR_SEND: 'P01f'  
    livingroomlight:
      friendly_name: living room light template
      value_template: '{{ states.switch.afstandbediening_2_205.state }}'
      turn_on:
        service: switch.mysensors_send_ir_code
        entity_id: switch.afstandbediening_2_205
        data:
          V_IR_SEND: 'P02t'
      turn_off:
        service: switch.mysensors_send_ir_code
        entity_id: switch.afstandbediening_2_205
        data:
          V_IR_SEND: 'P02f'  

then i get 3 switches. all connected.
if 1 turn on 1 then the others also turn on. or it turn immediatly off again.
and i also got a sensor with the same name.

i need a possibility to send a code to a sensor (or switch) by flipping a switch.

the reason why that is important:
right now i program the arduino for every switch i start to use. so in the sketch there are right now 11 switches.
every time i start using another switch, or when i change the setting in a switch, i have to reprogram the arduino.

if i could make a switch which sends a code then i could make a general arduino sketch which never needs changing.
when i put in another switch i just edit the configuration file and it will be working.

that way i can put a few arduinos all around the house and will Always be able to use every 433 mhz switch i start using, hust by changing the config.

after reading some more i found a workaround.
not the nicest one but it does the job.

with input_select i trigger an automation like this:

input_select:
  diningroomlight:
    name: diningroomlight
    options:
      - 'on'
      - 'off'
  livingroomlight:
    name: livingroomlight
    options:
      - 'on'
      - 'off'
automation 6:
  trigger:
    platform: state
    entity_id: input_select.diningroomlight
    to: 'off'
  action:
    service: switch.mysensors_send_ir_code
    entity_id: switch.afstandbediening_2_206
    data:
      V_IR_SEND: 'P01f'
automation 7:
  trigger:
    platform: state
    entity_id: input_select.diningroomlight
    to: 'on'
  action:
    service: switch.mysensors_send_ir_code
    entity_id: switch.afstandbediening_2_206
    data:
      V_IR_SEND: 'P01t'
automation 8:
  trigger:
    platform: state
    entity_id: input_select.livingroomlight
    to: 'off'
  action:
    service: switch.mysensors_send_ir_code
    entity_id: switch.afstandbediening_2_206
    data:
      V_IR_SEND: 'P02f'
automation 9:
  trigger:
    platform: state
    entity_id: input_select.livingroomlight
    to: 'on'
  action:
    service: switch.mysensors_send_ir_code
    entity_id: switch.afstandbediening_2_206
    data:
      V_IR_SEND: 'P02t'

to bad that the input_boolean doesnt function as a trigger as well. that would make it perfect.

I haven’t tested this but it should work as far as I can follow the source code. There could be some missing formatting required for the templates.

switch:
  platform: template
  switches:
    diningroomlight:
      friendly_name: diningroomlight
      entity_id: 'switch.afstandbediening_2_206'
      value_template: >
        {% if is_state_attr('switch.afstandbediening_2_206', 'V_IR_SEND', 'P01t') %}
          on
        {% elif is_state_attr('switch.afstandbediening_2_206', 'V_IR_SEND', 'P01f') %}
          off
        {% else %}
          "{{ states('switch.diningroomlight') }}"
        {% endif %}
      turn_on:
        service: switch.mysensors_send_ir_code
        entity_id: switch.afstandbediening_2_206
        data:
          V_IR_SEND: 'P01t'  
      turn_off:
        service: switch.turn_off
        entity_id: switch.afstandbediening_2_206
        data:
          V_IR_SEND: 'P01f'

The idea is that the template switch checks for a state update whenever the state of the IR switch changes. There are three conditions. If the ‘V_IR_SEND’ state attribute matches the code for the turn_on service, state will be ‘on’, if it matches the code for turn_off, state will be ‘off’, otherwise state will be the same.

You might get an error at upstart when the template switch initializes, due to it has an unknown state, which is not valid for template switches.

1 Like

i needed to lose the ‘’ around the on and off, but then it functioned, untill …

i made the second switch.

with this code the templateswitch changes to unavailable when the code is not P01t of P01f
and the second switch changes the code to P02f or P02t

like i said before, the template switch isnt gonna work if it is depending on the main switch (afstandbediening_2_206)

i tried to make an input_boolean and make the templateswitch connected to that.
but the boolean doesnt change when i change the template.

Did you get any errors in your log, when you made the second switch? It could help me pinpoint exactly the logic flow. As I’m reading the code, I still think this should work, but there might be something I’m not seeing.

yeah there are errors.
“Received invalid switch is_on state: “unavailable”. Expected: on, off, true, false”

i’d like you to be right. but i think it isnt possible this way.

there is only 1 varariable. afstandbediening_2.206.V_IR_SEND

if you make 1 switch there is no problem.

var==P01t >> switch1 on
var==P01f >> switch1 off

but with 2 switches you get:

var==P01t>>switch1 on AND switch 2 doesnt know what to do
var==P01f>>switch1 off AND switch 2 doesnt know what to do
var==P02t>>switch2 on AND switch 1 doesnt know what to do
var==P02f>>switch2 off AND switch 1 doesnt know what to do

the problem is that if you change the value from the var anywhere in the frontend or through automation, it changes everything which is depending on that var.

i really think that the only option is when the input_boolean also gets an state event like the input_select.